示例#1
0
 def getTopLevelObjects(self, allowSketches=False):
     #-------------------------------------------
     # Create treenodes of the importable Objects with a shape
     #-------------------------------------------
     self.treeNodes = {}
     shapeObs = a2plib.filterShapeObs(self.doc.Objects,allowSketches)
     
     S = set(shapeObs)
     for ob in S:
         inList = a2plib.filterShapeObs(ob.InList,allowSketches)
         outList = a2plib.filterShapeObs(ob.OutList,allowSketches)
         self.treeNodes[ob.Name] = (
                 inList,
                 outList
                 )
     #-------------------------------------------
     # nodes with empty inList are top level shapes for sure
     # (cloned objects could be missing)
     #-------------------------------------------
     self.topLevelShapes = []
     for objName in self.treeNodes.keys():
         inList,dummy = self.treeNodes[objName]
         if self.isTopLevelInList(inList):
             self.topLevelShapes.append(objName)
         elif allowSketches==True and objName.startswith('Sketch'): # want to have all sketches
             self.topLevelShapes.append(objName)
         else:
             #-------------------------------------------
             # search for missing non top-level clone-basefeatures
             # Maybe a clone as basefeature of a body..
             #-------------------------------------------
             numBodies = 0
             numClones = 0
             invalidObjects = False
             if len(inList) % 2 == 0: # pairs of Clone/Bodies
                 for o in inList:
                     if o.Name.startswith('Clone'):
                         numClones += 1
                     elif o.Name.startswith('Body'):
                         numBodies += 1
                     else:
                         invalidObjects = True
                         break
                 if not invalidObjects:
                     if numBodies == numClones:
                         self.topLevelShapes.append(objName)
             #-------------------------------------------
             # search for missing non top-level objects,
             # as they are referenced by fasteners WB objects
             #-------------------------------------------
             allObjectsAreFasteners = True
             for o in inList:
                 if not a2plib.isFastenerObject(o):
                     allObjectsAreFasteners = False
                     break
                 if allObjectsAreFasteners == True:
                     self.topLevelShapes.append(objName)
     #-------------------------------------------
     # Got some shapes used by sections?
     # collect them to a blacklist
     # InLists of objects used by section are empty, therefore they
     # are recognized falsely as topLevelShapes
     #
     # 2020-02-23: "Group" added to blackList
     #-------------------------------------------
     blackList = []
     for ob in self.doc.Objects:
         if ob.Name.startswith("Group"):
             blackList.append(ob.Name)
         elif ob.Name.startswith("Section"):
             if hasattr(ob,"Base"):
                 if ob.Base is not None:
                     blackList.append(ob.Base.Name)
             if hasattr(ob,"Tool"):
                 if ob.Tool is not None:
                     blackList.append(ob.Tool.Name)
     #-------------------------------------------
     # Got some shapes created by PathWB? filter out...
     # also filter out invisible shapes...
     # also filter out the blackList
     #-------------------------------------------
     tmp = []
     for n in self.topLevelShapes:
         if self.addedByPathWB(n): continue
         if n in blackList: continue
         #
         if a2plib.doNotImportInvisibleShapes():
             ob = self.doc.getObject(n)
             if hasattr(ob,"ViewObject"):
                 if hasattr(ob.ViewObject,"Visibility"):
                     if ob.ViewObject.Visibility == False or not a2plib.isGlobalVisible(ob):
                         print(
                             "Import ignored invisible shape! {}".format(
                                 ob.Name
                                 )
                               )
                         continue
         tmp.append(n)
     self.topLevelShapes = tmp
     #-------------------------------------------
     # return complete topLevel document objects for external use
     #-------------------------------------------
     outObs = []
     for objName in self.topLevelShapes:
         outObs.append(self.doc.getObject(objName))
     return outObs
示例#2
0
def muxAssemblyWithTopoNames(doc, desiredShapeLabel=None):
    """
    Mux an a2p assembly.

    combines all the a2p objects in the doc into one shape
    and populates muxinfo with a description of an edge or face.
    these descriptions are used later to retrieve the edges or faces...
    """
    faces = []
    faceColors = []
    muxInfo = []  # List of keys, not used at moment...

    visibleObjects = [
        obj for obj in doc.Objects
        if hasattr(obj, 'ViewObject') and obj.ViewObject.isVisible()
        and hasattr(obj, 'Shape') and len(obj.Shape.Faces) > 0
        and hasattr(obj, 'muxInfo') and a2plib.isGlobalVisible(obj)
    ]

    if desiredShapeLabel:  # is not None..
        tmp = []
        for ob in visibleObjects:
            if ob.Label == desiredShapeLabel:
                tmp.append(ob)
                break
        visibleObjects = tmp

    transparency = 0
    shape_list = []
    for obj in visibleObjects:
        extendNames = False
        if a2plib.getUseTopoNaming() and len(
                obj.muxInfo) > 0:  # Subelement-Strings existieren schon...
            extendNames = True
            #
            vertexNames = []
            edgeNames = []
            faceNames = []
            #
            for item in obj.muxInfo:
                if item[0] == 'V': vertexNames.append(item)
                if item[0] == 'E': edgeNames.append(item)
                if item[0] == 'F': faceNames.append(item)

        if a2plib.getUseTopoNaming():
            for i in range(0, len(obj.Shape.Vertexes)):
                if extendNames:
                    newName = "".join((vertexNames[i], obj.Name, ';'))
                    muxInfo.append(newName)
                else:
                    newName = "".join(('V;', str(i + 1), ';', obj.Name, ';'))
                    muxInfo.append(newName)
            for i in range(0, len(obj.Shape.Edges)):
                if extendNames:
                    newName = "".join((edgeNames[i], obj.Name, ';'))
                    muxInfo.append(newName)
                else:
                    newName = "".join(('E;', str(i + 1), ';', obj.Name, ';'))
                    muxInfo.append(newName)

        # Save Computing time, store this before the for..enumerate loop later...
        needDiffuseColorExtension = (len(obj.ViewObject.DiffuseColor) < len(
            obj.Shape.Faces))
        shapeCol = obj.ViewObject.ShapeColor
        diffuseCol = obj.ViewObject.DiffuseColor
        tempShape = makePlacedShape(obj)
        transparency = obj.ViewObject.Transparency
        shape_list.append(obj.Shape)

        # now start the loop with use of the stored values..(much faster)
        topoNaming = a2plib.getUseTopoNaming()
        diffuseElement = makeDiffuseElement(shapeCol, transparency)
        for i in range(0, len(tempShape.Faces)):
            if topoNaming:
                if extendNames:
                    newName = "".join((faceNames[i], obj.Name, ';'))
                    muxInfo.append(newName)
                else:
                    newName = "".join(('F;', str(i + 1), ';', obj.Name, ';'))
                    muxInfo.append(newName)
            if needDiffuseColorExtension:
                faceColors.append(diffuseElement)

        if not needDiffuseColorExtension:
            faceColors.extend(diffuseCol)

        faces.extend(tempShape.Faces)

    #if len(faces) == 1:
    #    shell = Part.makeShell([faces])
    #else:
    #    shell = Part.makeShell(faces)

    shell = Part.makeShell(faces)

    try:
        if a2plib.getUseSolidUnion():
            if len(shape_list) > 1:
                shape_base = shape_list[0]
                shapes = shape_list[1:]
                solid = shape_base.fuse(shapes)
            else:
                solid = Part.Solid(shape_list[0])
        else:
            solid = Part.Solid(
                shell
            )  # This does not work if shell includes spherical faces. FC-Bug ??
            # Fall back to shell if some faces are missing..
            if len(shell.Faces) != len(solid.Faces):
                solid = shell
    except:
        # keeping a shell if solid is failing
        FreeCAD.Console.PrintWarning('Union of Shapes FAILED\n')
        solid = shell

    # transparency could change to different values depending
    # on the order of imported objects
    # now set it to a default value
    # faceColors still contains the per face transparency values
    transparency = 0
    return muxInfo, solid, faceColors, transparency