This is not too bad. There are a few things to note though.
First is that you are modifying the hierarchy as you are walking it. (calling reverse face will change the verts of the face thus modifying the children of the face before you have visited them) This could cause bad behavior. If you moved your findHiddenPolys function to the Post Walk function mgWalk (db, None, findHiddenPolys ...) it would be more safe.
Second, you are not using counter from what I see. You could get rid of all lines that mention counter (pass None into the mgWalk instead of counter) and it would not change the behavior.
Ok the last thing I see and the most important is that you are comparing a floating point (1.0000...) to an integer (1) in the line if k == 1:
This could cause a lot of missed faces. for instance through rounding error 0.9999999999 would fail even though it is clearly pointing up. A better approach would be to compare it with a threshold. if k > 1 - 0.0001 and k < 1 + 0.0001:
You can use this threshold to also flip faces that point mostly up.
i was trying this out a couple of different ways. I had grabbed the counter from here somewhere,
it worked with what I was doing, so I ran with it in everything lately.
This is how I had it before I posted earlier. I combined the two defs into one and was going in that direction.
with this below, am I using the counter correctly?
def findHiddenPolys ( db, parent, rec , counter):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyHidden)
if (outs[2] == 1):
counter.match_list.append (rec)
counter.count = counter.count + 1
return MG_TRUE
def flipPolys(counter):
for rec in counter.match_list:
b,i,j,k = mgGetPolyNormal(rec)
if k == 1:
mgSelectOne(rec)
mgExecute("Reverse Face",None)
return MG_TRUE
return MG_TRUE
mgWalk(db,findHiddenPolys, None, counter, MWALK_NEXT)
flipPolys(counter)
del counter.match_list[:]
counter.count = 0
in my last post..the
mgSelectOne(rec) works on one face. i thought the mwalk would walk and keep looking for more..right?
i can go in and combine the footprint faces and the selectone work(as expected)
Ok, your last post makes more sense for the use of the counter. Yes the mgWalk would keep looking for more. The problem with your last post is in the flipPolys function.
You are doing two things wrong.
1) you are returning after the first one was flipped and ignoring all others with your call to return after mgExecute.
the flipPolys function is called only one time by you. you don't really need a return at all in that function as you are never checking or depending on the return
Note that findHiddenPolys does need the return. This is because the mgWalk starts a process where it calls your function for every node until a false is returned.
2) you are flipping after selecting and never deselect anything.
so flipPolys would execute like this:
select poly 1, flip poly 1
select poly 2, flip poly 1 and 2
select poly 3, flip poly 1, 2 and 3...
you can deselect all at the start of flip polys, select in the for loop then flip after the for loop just before returning
I had already started reworking the script and overwrote the file I had posted earlier. I went with the mgfind route.
The script works fine (stand alone, file open in creator) but will not work trying to use a mspromptdialog. it will do everything but the project and reverse...
with prompt(not working)
def findPolys2Project ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyFootprint)
if (outs[2] == 1):
mgDeselectAll(db)
mgSelectOne(rec)
mgExecute("Project",None)
def findPolys2Flip ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyFootprint)
if (outs[2] == 1):
b,i,j,k = mgGetPolyNormal(rec)
if k > 1 -0.0001 and k < +0.0001:
mgDeselectAll(db)
mgSelectOne(rec)
mgExecute("Reverse Face",None)
def finddblTaggedPolys ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyFootprint,fltPolyRoofline)
polyFtprint = outs[2]
polyRoofline = outs[2]
if (polyFtprint == 1):
if (polyRoofline == 1):
mgSelectOne(rec)
changes = mgSetAttList (rec,fltPolyRoofline,0)
return MG_TRUE
def renameFacesHidden ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyHidden)
if (outs[2] == 1):
namePattern = 'hidden'
oldName = mgGetName(rec)
newName = namePattern
b = mgSetNameUnique(rec,newName)
def renameFacesRoofline( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyRoofline)
if (outs[2] == 1):
namePattern = 'roofline'
oldName = mgGetName(rec)
newName = namePattern
b = mgSetNameUnique(rec,newName)
mgInit(None,None)
outs = mgPromptDialogFile (None,
MPFM_OPEN,
MPFA_FLAGS, MPFF_FILEMUSTEXIST|MPFF_MULTISELECT,
MPFA_PATTERN, "OpenFlight Files|*.flt",
MPFA_TITLE, "OpenFlight File" )
status = outs[0]
if (MSTAT_ISOK(status)):
numFiles = outs[1]
fileNames = outs[2:len(outs)]
for fileName in fileNames:
db = mgOpenDb (fileName)
projectList = mgFind(db,findPolys2Project,None,0)
flipList = mgFind(db,findPolys2Flip,None,0)
doubleTaggedList = mgFind(db,findDblTaggedPolys,None,0)
renameHiddenList = mgFind(db,renameFacesHidden,None,0)
renameRooflineList = mgFind(db,renameFacesRoofline,None,0)
mgWriteDb(db)
mgCloseDb(db)
###########################################################################################
stand alone( file open in creator to see results)
###########################################################################################
def findPolys2Project ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyFootprint)
if (outs[2] == 1):
mgDeselectAll(db)
mgSelectOne(rec)
mgExecute("Project",None)
def findPolys2Flip ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyFootprint)
if (outs[2] == 1):
b,i,j,k = mgGetPolyNormal(rec)
if k > 1 -0.0001 and k < +0.0001:
mgDeselectAll(db)
mgSelectOne(rec)
mgExecute("Reverse Face",None)
def finddblTaggedPolys ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyFootprint,fltPolyRoofline)
polyFtprint = outs[2]
polyRoofline = outs[2]
if (polyFtprint == 1):
if (polyRoofline == 1):
mgSelectOne(rec)
changes = mgSetAttList (rec,fltPolyRoofline,0)
return MG_TRUE
def renameFacesHidden ( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyHidden)
if (outs[2] == 1):
namePattern = 'hidden'
oldName = mgGetName(rec)
newName = namePattern
b = mgSetNameUnique(rec,newName)
def renameFacesRoofline( db, parent, rec , userData):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyRoofline)
if (outs[2] == 1):
namePattern = 'roofline'
oldName = mgGetName(rec)
newName = namePattern
b = mgSetNameUnique(rec,newName)
mgInit(None,None)
db = mgGetCurrentDb()
projectList = mgFind(db,findPolys2Project,None,0)
flipList = mgFind(db,findPolys2Flip,None,0)
doubleTaggedList = mgFind(db,findDblTaggedPolys,None,0)
renameHiddenList = mgFind(db,renameFacesHidden,None,0)
renameRooflineList = mgFind(db,renameFacesRoofline,None,0)
mgWriteDb(db)
mgCloseDb(db)
John
this is from my last script that seemed to be working but i ran into a model with a face in +z and in -z.
Im not really sure if what im trying to do here is right at all.
this is run on a few footprint polygons facing +z. i want this to select them all and reverse them and if there are polys already facing -z, leave them alone
i was thinking the k == 1 meant the polys that are facing +z would be selected and reversed
def findHiddenPolys ( db, parent, rec , counter):
type = mgGetCode (rec)
if ( type == fltPolygon ):
outs = mgGetAttList (rec, fltPolyHidden)
if (outs[2] == 1):
counter.match_list.append (rec)
counter.count = counter.count + 1
mgDeselectAll(db)
for rec in counter.match_list:
b,i,j,k = mgGetPolyNormal(rec)
if k == 1:
mgSelectOne(rec)
mgExecute("Reverse Face",None)
return MG_TRUE
return MG_TRUE
mgWalk(db,findHiddenPolys, None, counter, MWALK_NEXT)