Exemplo n.º 1
0
    def createTopoNames(self, desiredShapeLabel = None):
        '''
        creates a combined shell of all toplevel objects and
        assigns toponames to its geometry if toponaming is
        enabled.
        '''
        self.detectPartDesignDocument()
        self.getTopLevelObjects()
        
        # filter topLevelShapes if there is a desiredShapeLabel 
        # means: extract only one desired shape out of whole file...
        if desiredShapeLabel: #is not None
            tmp = []
            for objName in self.topLevelShapes:
                o = self.doc.getObject(objName)
                if o.Label == desiredShapeLabel:
                    tmp.append(o.Name)
            self.topLevelShapes = tmp
        
        #-------------------------------------------
        # analyse the toplevel shapes
        #-------------------------------------------
        if a2plib.getUseTopoNaming():
            for n in self.topLevelShapes:
                self.totalNumVertexes = 0
                self.totalNumEdges = 0
                self.totalNumFaces = 0
                self.processTopoData(n) # analyse each toplevel object...
        #
        #-------------------------------------------
        # MUX the toplevel shapes
        #-------------------------------------------
        faces = []
        faceColors = []
        transparency = 0
        shape_list = []
        
        for objName in self.topLevelShapes:
            ob = self.doc.getObject(objName)
            needDiffuseExtension = ( len(ob.ViewObject.DiffuseColor) < len(ob.Shape.Faces) )
            shapeCol = ob.ViewObject.ShapeColor
            diffuseCol = ob.ViewObject.DiffuseColor
            tempShape = self.makePlacedShape(ob)
            transparency = ob.ViewObject.Transparency
            shape_list.append(ob.Shape)
            
            if needDiffuseExtension:
                diffuseElement = a2plib.makeDiffuseElement(shapeCol,transparency)
                for i in range(0,len(tempShape.Faces)):
                    faceColors.append(diffuseElement)
            else:
                faceColors.extend(diffuseCol) #let python libs extend faceColors, much faster
            faces.extend(tempShape.Faces) #let python libs extend faces, much faster

        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:   #one shape only
                    solid = Part.Solid(shape_list[0])
            else:
                solid = Part.Solid(shell) # fails with missing faces if shell contains spheres
                if len(shell.Faces) != len(solid.Faces):
                    solid = shell # fall back to shell if faces are missing
        except:
            # keeping a shell if solid is failing
            FreeCAD.Console.PrintWarning('Union of Shapes FAILED\n')
            solid = shell
        
        #-------------------------------------------
        # if toponaming is used, assign toponames to
        # shells geometry
        #-------------------------------------------
        muxInfo = []
        if a2plib.getUseTopoNaming():
            #-------------------------------------------
            # map vertexnames to the MUX
            #-------------------------------------------
            muxInfo.append("[VERTEXES]")
            for i,v in enumerate(solid.Vertexes):
                k = self.calcVertexKey(v)
                name = self.shapeDict.get(k,"None")
                muxInfo.append(name)
            #-------------------------------------------
            # map edgenames to the MUX
            #-------------------------------------------
            muxInfo.append("[EDGES]")
            pl = FreeCAD.Placement()
            for i,edge in enumerate(solid.Edges):
                keys = self.calcEdgeKeys(edge, pl)
                name = self.shapeDict.get(keys[0],"None")
                muxInfo.append(name)
            #-------------------------------------------
            # map facenames to the MUX
            #-------------------------------------------
            muxInfo.append("[FACES]")
            pl = FreeCAD.Placement()
            for i,face in enumerate(solid.Faces):
                keys = self.calcFaceKeys(face, pl)
                name = self.shapeDict.get(keys[0],"None")
                muxInfo.append(name)


        return muxInfo, solid, faceColors, transparency
Exemplo n.º 2
0
    def createTopoNames(self, desiredShapeLabel=None):
        '''
        creates a combined shell of all toplevel objects and
        assigns toponames to its geometry if toponaming is
        enabled.
        '''
        if desiredShapeLabel is not None:
            allowSketches = True
        else:
            allowSketches = False

        self.detectPartDesignDocument()
        self.getTopLevelObjects(allowSketches)

        # filter topLevelShapes if there is a desiredShapeLabel
        # means: extract only one desired shape out of whole file...
        if desiredShapeLabel:  #is not None
            tmp = []
            for objName in self.topLevelShapes:
                o = self.doc.getObject(objName)
                if o.Label == desiredShapeLabel:
                    tmp.append(o.Name)
            self.topLevelShapes = tmp

        #-------------------------------------------
        # analyse the toplevel shapes
        #-------------------------------------------
        if a2plib.getUseTopoNaming():
            for n in self.topLevelShapes:
                self.totalNumVertexes = 0
                self.totalNumEdges = 0
                self.totalNumFaces = 0
                self.processTopoData(n)  # analyse each toplevel object...
        #
        #-------------------------------------------
        # MUX the toplevel shapes
        #-------------------------------------------
        faces = []
        faceColors = []
        transparency = 0
        shape_list = []

        if len(self.topLevelShapes) == 1 and self.topLevelShapes[0].startswith(
                "Sketch"):
            importingSketch = True
        else:
            importingSketch = False

        if importingSketch == True:
            # We are importing a sketch object
            objName = self.topLevelShapes[0]
            sketchOb = self.doc.getObject(objName)
            solid = sketchOb.Shape
        else:
            # We are importing no sketch object
            for objName in self.topLevelShapes:
                ob = self.doc.getObject(objName)
                tempShape = self.makePlacedShape(ob)
                faces.extend(tempShape.Faces
                             )  #let python libs extend faces, much faster

                #manage colors of faces
                if ob.ViewObject.TypeId == "Gui::ViewProviderLinkPython":  # a link is involved...
                    linkedObject = self.getLinkedObjectRecursive(ob)
                    needDiffuseExtension = (len(
                        linkedObject.ViewObject.DiffuseColor) < len(
                            linkedObject.Shape.Faces))
                    shapeCol = linkedObject.ViewObject.ShapeColor
                    diffuseCol = linkedObject.ViewObject.DiffuseColor
                    transparency = linkedObject.ViewObject.Transparency
                    shape_list.append(ob.Shape)

                    if needDiffuseExtension:
                        diffuseElement = a2plib.makeDiffuseElement(
                            shapeCol, transparency)
                        for i in range(0, len(tempShape.Faces)):
                            faceColors.append(diffuseElement)
                    else:
                        count = len(ob.Shape.Faces) // len(
                            linkedObject.Shape.Faces)
                        for c in range(
                                0, count
                        ):  # add colors to multiple representations of linkedObject
                            faceColors.extend(diffuseCol)
                else:  # no link is involved...
                    needDiffuseExtension = (len(ob.ViewObject.DiffuseColor) <
                                            len(ob.Shape.Faces))
                    shapeCol = ob.ViewObject.ShapeColor
                    diffuseCol = ob.ViewObject.DiffuseColor
                    transparency = ob.ViewObject.Transparency
                    shape_list.append(ob.Shape)
                    if needDiffuseExtension:
                        diffuseElement = a2plib.makeDiffuseElement(
                            shapeCol, transparency)
                        for i in range(0, len(tempShape.Faces)):
                            faceColors.append(diffuseElement)
                    else:
                        faceColors.extend(
                            diffuseCol
                        )  #let python libs extend faceColors, much faster

            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:  #one shape only
                        solid = Part.Solid(shape_list[0])
                else:
                    solid = Part.Solid(
                        shell
                    )  # fails with missing faces if shell contains spheres
                    if len(shell.Faces) != len(solid.Faces):
                        solid = shell  # fall back to shell if faces are missing
            except:
                # keeping a shell if solid is failing
                FreeCAD.Console.PrintWarning('Union of Shapes FAILED\n')
                solid = shell

        #-------------------------------------------
        # if toponaming is used, assign toponames to
        # shells geometry
        #-------------------------------------------
        muxInfo = []
        if a2plib.getUseTopoNaming():
            #-------------------------------------------
            # map vertexnames to the MUX
            #-------------------------------------------
            muxInfo.append("[VERTEXES]")
            for i, v in enumerate(solid.Vertexes):
                k = self.calcVertexKey(v)
                defaultVal = "V;NONAME;{};".format(i)
                name = self.shapeDict.get(k, defaultVal)
                muxInfo.append(name)
            #-------------------------------------------
            # map edgenames to the MUX
            #-------------------------------------------
            muxInfo.append("[EDGES]")
            pl = FreeCAD.Placement()
            for i, edge in enumerate(solid.Edges):
                keys = self.calcEdgeKeys(edge, pl)
                defaultVal = "E;NONAME;{};".format(i)
                name = self.shapeDict.get(keys[0], defaultVal)
                muxInfo.append(name)
            #-------------------------------------------
            # map facenames to the MUX
            #-------------------------------------------
            muxInfo.append("[FACES]")
            pl = FreeCAD.Placement()
            for i, face in enumerate(solid.Faces):
                keys = self.calcFaceKeys(face, pl)
                defaultVal = "F;NONAME;{};".format(i)
                name = self.shapeDict.get(keys[0], defaultVal)
                muxInfo.append(name)

        return muxInfo, solid, faceColors, transparency