Пример #1
0
def createOrUpdateSimpleAssemblyShape(doc):
    visibleImportObjects = [
        obj for obj in doc.Objects if 'importPart' in obj.Content
        and hasattr(obj, 'ViewObject') and obj.ViewObject.isVisible()
        and hasattr(obj, 'Shape') and len(obj.Shape.Faces) > 0
    ]

    if len(visibleImportObjects) == 0:
        QtGui.QMessageBox.critical(QtGui.QApplication.activeWindow(),
                                   "Cannot create SimpleAssemblyShape",
                                   "No visible ImportParts found")
        return

    sas = doc.getObject('SimpleAssemblyShape')
    if sas == None:
        sas = doc.addObject("Part::FeaturePython", "SimpleAssemblyShape")
        SimpleAssemblyShape(sas)
        #sas.ViewObject.Proxy = 0
        ViewProviderSimpleAssemblyShape(sas.ViewObject)
    faces = []
    shape_list = []
    for obj in visibleImportObjects:
        faces = faces + obj.Shape.Faces
        shape_list.append(obj.Shape)
    shell = Part.makeShell(faces)
    try:
        # solid = Part.Solid(shell)
        # solid = Part.makeCompound (shape_list)
        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
                numShellFaces = len(shell.Faces)
                solid = Part.Solid(
                    shell
                )  # This does not work if shell includes spherical faces. FC-Bug ??
                numSolidFaces = len(solid.Faces)
                # Check, whether all faces where processed...
                if numShellFaces != numSolidFaces:
                    solid = shell  # Some faces are missing, take shell as result as workaround..
        else:
            numShellFaces = len(shell.Faces)
            solid = Part.Solid(
                shell
            )  # This does not work if shell includes spherical faces. FC-Bug ??
            numSolidFaces = len(solid.Faces)
            # Check, whether all faces where processed...
            if numShellFaces != numSolidFaces:
                solid = shell  # Some faces are missing, take shell as result as workaround..
    except:
        # keeping a shell if solid is failing
        solid = shell
    sas.Shape = solid  #shell
    sas.ViewObject.Visibility = False
Пример #2
0
def createOrUpdateSimpleAssemblyShape(doc):
    visibleImportObjects = [
        obj for obj in doc.Objects if 'importPart' in obj.Content
        and hasattr(obj, 'ViewObject') and obj.ViewObject.isVisible()
        and hasattr(obj, 'Shape') and len(obj.Shape.Faces) > 0
    ]

    if len(visibleImportObjects) == 0:
        QtGui.QMessageBox.critical(
            QtGui.QApplication.activeWindow(),
            translate("A2plus_MuxAssembly",
                      "Cannot create SimpleAssemblyShape"),
            translate("A2plus_MuxAssembly", "No visible ImportParts found"))
        return

    sas = doc.getObject('SimpleAssemblyShape')
    if sas is None:
        sas = doc.addObject("Part::FeaturePython", "SimpleAssemblyShape")
        SimpleAssemblyShape(sas)
        #sas.ViewObject.Proxy = 0
        ViewProviderSimpleAssemblyShape(sas.ViewObject)
    faces = []
    shape_list = []
    for obj in visibleImportObjects:
        faces = faces + obj.Shape.Faces
        shape_list.append(obj.Shape)
    if len(faces) == 1:
        shell = Part.makeShell([faces])
    else:
        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 faces are misiing
            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
    sas.Shape = solid  #shell
    sas.ViewObject.Visibility = False
Пример #3
0
def createOrUpdateSimpleAssemblyShape(doc):
    visibleImportObjects = [
        obj for obj in doc.Objects if 'importPart' in obj.Content
        and hasattr(obj, 'ViewObject') and obj.ViewObject.isVisible()
        and hasattr(obj, 'Shape') and len(obj.Shape.Faces) > 0
    ]

    if len(visibleImportObjects) == 0:
        QtGui.QMessageBox.critical(QtGui.QApplication.activeWindow(),
                                   "Cannot create SimpleAssemblyShape",
                                   "No visible ImportParts found")
        return

    sas = doc.getObject('SimpleAssemblyShape')
    if sas == None:
        sas = doc.addObject("Part::FeaturePython", "SimpleAssemblyShape")
        SimpleAssemblyShape(sas)
        #sas.ViewObject.Proxy = 0
        ViewProviderSimpleAssemblyShape(sas.ViewObject)
    faces = []
    shape_list = []
    for obj in visibleImportObjects:
        faces = faces + obj.Shape.Faces
        shape_list.append(obj.Shape)
    shell = Part.makeShell(faces)
    try:
        # solid = Part.Solid(shell)
        # solid = Part.makeCompound (shape_list)
        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 = shape_list[0]
        else:
            solid = Part.Solid(shell)
    except:
        # keeping a shell if solid is failing
        solid = shell
    sas.Shape = solid  #shell
    sas.ViewObject.Visibility = False
Пример #4
0
def muxAssemblyWithTopoNames(doc):
    '''
    Mux an a2p assenbly

    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')
    ]

    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)

    shell = Part.makeShell(faces)
    try:
        # solid = Part.Solid(shell)
        # solid = Part.makeCompound (shape_list)
        if a2plib.getUseSolidUnion():
            if len(shape_list) > 1:
                shape_base = shape_list[0]
                shapes = shape_list[1:]
                solid = shape_base.fuse(shapes)
            else:  #one drill ONLY
                solid = shape_list[0]
        else:
            numShellFaces = len(shell.Faces)
            solid = Part.Solid(
                shell
            )  # This does not work if shell includes spherical faces. FC-Bug ??
            numSolidFaces = len(solid.Faces)
            # Check, whether all faces where processed...
            if numShellFaces != numSolidFaces:
                solid = shell  # Some faces are missing, take shell as result as workaround..
    except:
        # keeping a shell if solid is failing
        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
Пример #5
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
Пример #6
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
Пример #7
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