Start a new topic

Concave Polygon Fix

Original Post by: maruthi Tue Jan 27 13:13:29 2015


Hi all,

I wrote script to Fix Concave Polygons, I hope it is very useful to fixing concave poly.


Thanks,

Maruthi.B


#Thank you for using my Script

#Developed by Maruthi.B

#Mail:maruthikrishnan@yahoo.com


#description:

#.Select concave polygon to run

#.It will make proper polygon without any concave

#.It will work on creator 4.2 and later


db = mgGetCurrentDb()


class indentcl:

pass

cl = indentcl

cl.listPoly=[]


def listSelectedPolygon():

listPoly = mgGetSelectList(db)

mgDeselectAll(db)

cl.listPoly=[]

if listPoly:

rec,x = mgGetNextRecInList (listPoly)

while rec:

cl.listPoly.append(rec)

rec,x = mgGetNextRecInList (listPoly)

else:

mgSendMessage( MMSG_STATUS,"None of the polygon is selected")


def polyMerge(p1,p2,vtx1,vtx2):

dupchild = []

parent = mgGetParent(p1)

rec = mgDuplicate(p1)

mgAttach(parent,rec)

localcount =1

child = mgGetChild(p2)

while child:

dupchild.insert((len(dupchild)),mgDuplicate(child))

localcount = localcount+1

child =mgGetNext(child)

localcount =1

child = mgGetChild(rec)

indexcheck =0

indexcheck1 =0

childCount = mgCountChild(p1)

if childCount in vtx1 and 1 in vtx1:

indexcheck = childCount

indexcheck1 = vtx2[1]

else:

indexcheck =vtx1[0]

indexcheck1 =vtx2[0]

for getvtx in range(len(dupchild)):

if getvtx > indexcheck1-2:

break

else:

getpop = dupchild.pop(0)

dupchild.insert(len(dupchild),getpop)

dupchild.pop(0)

dupchild.pop(len(dupchild)-1)

localcount=1

count2=1

child = mgGetChild(rec)

while child:

if localcount == indexcheck:

for dupch in range(len(dupchild)):

if dupch ==0:

mgInsert(child,dupchild[dupch])

else:

mgInsert(dupchild[dupch-1],dupchild[dupch])

localcount = localcount+1

child =mgGetNext(child)

mgDeselectAll(db)

mgSelectOne(rec)

mgExecute ("Combine Faces", None)


if mgIsPolyConcave(rec) ==1:

mgDetach(rec)

mgDelete(rec)

return 0

else:

mgDetach(p1)

mgDelete(p1)

mgDetach(p2)

mgDelete(p2)

return 1


def polyCheck(p1,p2):

p1list =[]

p2list =[]

p2Vtx =[]

p1Vtx =[]


if mgCountChild(p1) > 2 and mgCountChild(p2) > 2:

p1child = mgGetChild(p1)

while p1child:

p1list.append(mgGetVtxCoord(p1child))

p1child = mgGetNext(p1child)

p2child = mgGetChild(p2)

while p2child:

p2list.append(mgGetVtxCoord(p2child))

p2child = mgGetNext(p2child)


count =0

for x in range(len(p1list)):

for s in range(len(p2list)):

if p1list[x] == p2list[s]:

count = count+1

p2Vtx.append(s+1)

p1Vtx.append(x+1)

if count ==2:

return polyMerge(p1,p2,p1Vtx,p2Vtx)

return 0


def polyLoop(emptyGrp):

result =1

childFirst = mgGetChild(emptyGrp)

child = mgGetChild(emptyGrp)

while child:

result = polyCheck(childFirst,mgGetNext(child))

if result:

break

child = mgGetNext(child)

if not result:

mgDetach(childFirst)

mgAttach(cl.parent,childFirst)


def start(emptyGrp):

if mgCountChild(emptyGrp) !=0:

while mgCountChild(emptyGrp) > 1:

polyLoop(emptyGrp)

else:

rec =mgGetChild(emptyGrp)

mgDetach(rec)

mgAttach(cl.parent,rec)


def TriangulateOne (polygon,grp):

vertexList = mgGetTriangleList (polygon)

numVertex = mgGetRecListCount (vertexList)

if (numVertex > 3):

numTriangles = numVertex / 3;

for j in range (1, numTriangles+1):

vtx1,matrix = mgGetNextRecInList (vertexList)

vtx2,matrix = mgGetNextRecInList (vertexList)

vtx3,matrix = mgGetNextRecInList (vertexList)

newPoly = mgDuplicateEx (polygon, MDUP_NODEONLY)

mgAttach(grp,newPoly)

newVtx1 = mgDuplicate (vtx1)

newVtx2 = mgDuplicate (vtx2)

newVtx3 = mgDuplicate (vtx3)

mgAttach (newPoly, newVtx3)

mgAttach (newPoly, newVtx2)

mgAttach (newPoly, newVtx1)

def polyConcaveRemove():

listSelectedPolygon()

emptyGrp = mgNewRec(fltGroup)

if cl.listPoly:

for rec in cl.listPoly:

mgDeselectAll(db)

if mgIsPolyConcave(rec) ==1:

cl.parent =mgGetParent(rec)

mgDetach(rec)

mgAttach(emptyGrp,rec)

TriangulateOne(rec,emptyGrp)

mgDelete(rec)

start(emptyGrp)

mgDelete(emptyGrp)

mgDeselectAll(db)

polyConcaveRemove()



1 person has this problem
1 Comment

Original Post by: SteveThompson Tue Jan 27 17:41:24 2015


Thanks for sharing this. I bet others will find this useful!

Login to post a comment