def create(self,selection): cName = findUnusedObjectName(self.constraintBaseName) ob = FreeCAD.activeDocument().addObject("App::FeaturePython", cName) s1, s2 = selection self.ob1Name = s1.ObjectName self.ob2Name = s2.ObjectName self.ob1Label = s1.Object.Label self.ob2Label = s2.Object.Label self.ob1 = FreeCAD.activeDocument().getObject(s1.ObjectName) self.ob2 = FreeCAD.activeDocument().getObject(s2.ObjectName) self.sub1 = s1.SubElementNames[0] self.sub2 = s2.SubElementNames[0] ob.addProperty("App::PropertyString","Type","ConstraintInfo").Type = self.typeInfo ob.addProperty("App::PropertyString","Object1","ConstraintInfo").Object1 = self.ob1Name ob.addProperty("App::PropertyString","SubElement1","ConstraintInfo").SubElement1 = self.sub1 ob.addProperty("App::PropertyString","Object2","ConstraintInfo").Object2 = self.ob2Name ob.addProperty("App::PropertyString","SubElement2","ConstraintInfo").SubElement2 = self.sub2 ob.addProperty("App::PropertyString","Toponame1","ConstraintInfo").Toponame1 = '' ob.addProperty("App::PropertyString","Toponame2","ConstraintInfo").Toponame2 = '' ob.addProperty("App::PropertyBool","Suppressed","ConstraintInfo").Suppressed = False for prop in ["Object1","Object2","SubElement1","SubElement2","Type"]: ob.setEditorMode(prop, 1) self.constraintObject = ob self.calcInitialValues() #override in subclass ! self.setInitialValues() self.groupUnderParentTreeObject() self.setupProxies()
def duplicateImportedPart( part ): doc = FreeCAD.ActiveDocument nameBase = part.Label partName = a2plib.findUnusedObjectName(nameBase,document=doc) partLabel = a2plib.findUnusedObjectLabel(nameBase,document=doc) newObj = doc.addObject("Part::FeaturePython", partName) newObj.Label = partLabel # if hasattr(part,'a2p_Version'): newObj.addProperty("App::PropertyString", "a2p_Version","importPart").a2p_Version = part.a2p_Version newObj.addProperty("App::PropertyFile", "sourceFile", "importPart").sourceFile = part.sourceFile newObj.addProperty("App::PropertyFloat", "timeLastImport","importPart").timeLastImport = part.timeLastImport newObj.setEditorMode("timeLastImport",1) newObj.addProperty("App::PropertyBool","fixedPosition","importPart").fixedPosition = False# part.fixedPosition newObj.addProperty("App::PropertyBool","updateColors","importPart").updateColors = getattr(part,'updateColors',True) if hasattr(part, "muxInfo"): newObj.addProperty("App::PropertyStringList","muxInfo","importPart").muxInfo = part.muxInfo if hasattr(part, 'subassemblyImport'): newObj.addProperty("App::PropertyBool","subassemblyImport","importPart").subassemblyImport = part.subassemblyImport newObj.Shape = part.Shape.copy() for p in part.ViewObject.PropertiesList: #assuming that the user may change the appearance of parts differently depending on their role in the assembly. if hasattr(newObj.ViewObject, p) and p not in ['DiffuseColor','Proxy','MappedColors']: setattr(newObj.ViewObject, p, getattr( part.ViewObject, p)) newObj.ViewObject.DiffuseColor = copy.copy( part.ViewObject.DiffuseColor ) newObj.Proxy = Proxy_importPart() newObj.ViewObject.Proxy = ImportedPartViewProviderProxy() newObj.Placement.Base = part.Placement.Base newObj.Placement.Rotation = part.Placement.Rotation return newObj
def convertToImportedPart(doc, obj): """ convertToImportedPart(document, documentObject) - changes a regular FreeCAD object into an A2plus importedPart, adds the importedPart to the document and hides the original object from the document. Updating the assembly will also update the converted part """ partName = a2plib.findUnusedObjectName(obj.Label, document=doc) partLabel = a2plib.findUnusedObjectLabel(obj.Label, document=doc) filename = "converted" #or none? if obj is already in this doc, we don't know it's original filename newObj = doc.addObject("Part::FeaturePython", partName) newObj.Label = partLabel Proxy_importPart(newObj) ImportedPartViewProviderProxy(newObj.ViewObject) newObj.a2p_Version = A2P_VERSION newObj.sourceFile = filename newObj.localSourceObject = obj.Name #newObj.sourcePart = "" newObj.setEditorMode("timeLastImport", 1) newObj.timeLastImport = time.time() newObj.fixedPosition = False newObj.subassemblyImport = False newObj.setEditorMode("subassemblyImport", 1) newObj.updateColors = True newObj.ViewObject.ShapeColor = obj.ViewObject.ShapeColor #------------------------------------------- # Initialize the new TopoMapper #------------------------------------------- topoMapper = TopoMapper(doc) newObj.muxInfo, newObj.Shape, newObj.ViewObject.DiffuseColor, newObj.ViewObject.Transparency = \ topoMapper.createTopoNames(desiredShapeLabel = obj.Label) for p in obj.ViewObject.PropertiesList: if hasattr(obj.ViewObject, p) and p not in [ 'DiffuseColor', 'Proxy', 'MappedColors', 'DisplayModeBody' ]: try: setattr(newObj.ViewObject, p, getattr(obj.ViewObject, p)) except: #some sketcher attributes e.g. pass if not a2plib.getPerFaceTransparency(): # switch of perFaceTransparency newObj.ViewObject.Transparency = 1 newObj.ViewObject.Transparency = 0 # default = nontransparent newObj.Placement.Base = obj.Placement.Base newObj.Placement.Rotation = obj.Placement.Rotation obj.ViewObject.Visibility = False newObj.recompute()
def convertToImportedPart(doc, obj): ''' convertToImportedPart(document, documentObject) - changes a regular FreeCAD object into an A2plus importedPart, adds the importedPart to the document and removes the FreeCAD object from the document. Returns None ''' partName = a2plib.findUnusedObjectName(obj.Label, document=doc) partLabel = a2plib.findUnusedObjectLabel(obj.Label, document=doc) filename = "converted" #or none? if obj is already in this doc, we don't know it's original filename newObj = doc.addObject("Part::FeaturePython", partName) newObj.Label = partLabel newObj.Proxy = Proxy_convertPart() newObj.ViewObject.Proxy = ImportedPartViewProviderProxy() newObj.addProperty("App::PropertyString", "a2p_Version", "importPart").a2p_Version = A2P_VERSION newObj.addProperty("App::PropertyFile", "sourceFile", "importPart").sourceFile = filename newObj.addProperty("App::PropertyStringList", "muxInfo", "importPart") newObj.addProperty("App::PropertyFloat", "timeLastImport", "importPart") newObj.setEditorMode("timeLastImport", 1) newObj.timeLastImport = time.time() newObj.addProperty("App::PropertyBool", "fixedPosition", "importPart") newObj.fixedPosition = False newObj.addProperty("App::PropertyBool", "subassemblyImport", "importPart").subassemblyImport = False newObj.setEditorMode("subassemblyImport", 1) newObj.addProperty("App::PropertyBool", "updateColors", "importPart").updateColors = True newObj.Shape = obj.Shape.copy() newObj.muxInfo = createTopoInfo(obj) for p in obj.ViewObject.PropertiesList: if hasattr(obj.ViewObject, p) and p not in ['DiffuseColor', 'Proxy', 'MappedColors']: setattr(newObj.ViewObject, p, getattr(obj.ViewObject, p)) newObj.ViewObject.ShapeColor = obj.ViewObject.ShapeColor newObj.ViewObject.DiffuseColor = copy.copy( obj.ViewObject.DiffuseColor) # diffuse needs to happen last if not a2plib.getPerFaceTransparency(): # switch of perFaceTransparency newObj.ViewObject.Transparency = 1 newObj.ViewObject.Transparency = 0 # default = nontransparent newObj.Placement.Base = obj.Placement.Base newObj.Placement.Rotation = obj.Placement.Rotation doc.removeObject(obj.Name) # don't want the original in this doc anymore newObj.recompute()
def importPartFromFile(_doc, filename, importToCache=False): doc = _doc #------------------------------------------- # Get the importDocument #------------------------------------------- importDocIsOpen = filename in [ d.FileName for d in FreeCAD.listDocuments().values() ] if importDocIsOpen: importDoc = [ d for d in FreeCAD.listDocuments().values() if d.FileName == filename][0] else: if filename.lower().endswith('.fcstd'): importDoc = FreeCAD.openDocument(filename) else: msg = "A part can only be imported from a FreeCAD '*.fcstd' file" QtGui.QMessageBox.information( QtGui.QApplication.activeWindow(), "Value Error", msg ) return #------------------------------------------- # Get a list of the visible Objects #------------------------------------------- visibleObjects = [ obj for obj in importDoc.Objects if hasattr(obj,'ViewObject') and obj.ViewObject.isVisible() and hasattr(obj,'Shape') and len(obj.Shape.Faces) > 0 and 'Body' not in obj.Name] if visibleObjects == None or len(visibleObjects) == 0: msg = "No visible Part to import found. Aborting operation" QtGui.QMessageBox.information( QtGui.QApplication.activeWindow(), "Import Error", msg ) return #------------------------------------------- # Discover whether we are importing a subassembly or a single part #------------------------------------------- #if any([ 'importPart' in obj.Content for obj in importDoc.Objects]) and not len(visibleObjects) == 1: subAssemblyImport = False if len(visibleObjects) > 1: subAssemblyImport = True #------------------------------------------- # create new object #------------------------------------------- if importToCache: partName = 'CachedObject_'+str(objectCache.len()) newObj = doc.addObject("Part::FeaturePython",partName) newObj.Label = partName else: partName = a2plib.findUnusedObjectName( importDoc.Label, document=doc ) partLabel = a2plib.findUnusedObjectLabel( importDoc.Label, document=doc ) newObj = doc.addObject("Part::FeaturePython",partName.encode('utf-8')) newObj.Label = partLabel newObj.addProperty("App::PropertyString", "a2p_Version","importPart").a2p_Version = A2P_VERSION newObj.addProperty("App::PropertyFile", "sourceFile", "importPart").sourceFile = filename newObj.addProperty("App::PropertyStringList","muxInfo","importPart") newObj.addProperty("App::PropertyFloat", "timeLastImport","importPart") newObj.setEditorMode("timeLastImport",1) newObj.timeLastImport = os.path.getmtime( filename ) newObj.addProperty("App::PropertyBool","fixedPosition","importPart") newObj.fixedPosition = not any([i.fixedPosition for i in doc.Objects if hasattr(i, 'fixedPosition') ]) newObj.addProperty("App::PropertyBool","subassemblyImport","importPart").subassemblyImport = subAssemblyImport newObj.setEditorMode("subassemblyImport",1) newObj.addProperty("App::PropertyBool","updateColors","importPart").updateColors = True # if subAssemblyImport: newObj.muxInfo, newObj.Shape, newObj.ViewObject.DiffuseColor = muxObjectsWithKeys(importDoc, withColor=True) #newObj.muxInfo, newObj.Shape = muxObjectsWithKeys(importDoc, withColor=False) else: tmpObj = visibleObjects[0] newObj.Shape = tmpObj.Shape.copy() newObj.ViewObject.ShapeColor = tmpObj.ViewObject.ShapeColor if appVersionStr() <= '000.016': #FC0.17: DiffuseColor overrides ShapeColor ! newObj.ViewObject.DiffuseColor = tmpObj.ViewObject.DiffuseColor newObj.muxInfo = createTopoInfo(tmpObj) newObj.Proxy = Proxy_muxAssemblyObj() newObj.ViewObject.Proxy = ImportedPartViewProviderProxy() doc.recompute() if importToCache: objectCache.add(newObj.sourceFile, newObj) if not importDocIsOpen: FreeCAD.closeDocument(importDoc.Name) return newObj
def importPartFromFile(_doc, filename, importToCache=False): doc = _doc #------------------------------------------- # Get the importDocument #------------------------------------------- # look only for filenames, not paths, as there are problems on WIN10 (Address-translation??) importDoc = None importDocIsOpen = False requestedFile = os.path.split(filename)[1] for d in FreeCAD.listDocuments().values(): recentFile = os.path.split(d.FileName)[1] if requestedFile == recentFile: importDoc = d # file is already open... importDocIsOpen = True break if not importDocIsOpen: if filename.lower().endswith('.fcstd'): importDoc = FreeCAD.openDocument(filename) else: msg = "A part can only be imported from a FreeCAD '*.fcstd' file" QtGui.QMessageBox.information(QtGui.QApplication.activeWindow(), "Value Error", msg) return #------------------------------------------- # Initialize the new TopoMapper #------------------------------------------- topoMapper = TopoMapper(importDoc) #------------------------------------------- # Get a list of the importable Objects #------------------------------------------- importableObjects = topoMapper.getTopLevelObjects() if len(importableObjects) == 0: msg = "No visible Part to import found. Aborting operation" QtGui.QMessageBox.information(QtGui.QApplication.activeWindow(), "Import Error", msg) return #------------------------------------------- # Discover whether we are importing a subassembly or a single part #------------------------------------------- subAssemblyImport = False if all(['importPart' in obj.Content for obj in importableObjects]) == 1: subAssemblyImport = True ''' #------------------------------------------- # Discover whether we are importing a subassembly or a single part #------------------------------------------- #if any([ 'importPart' in obj.Content for obj in importDoc.Objects]) and not len(visibleObjects) == 1: subAssemblyImport = False Msg("A2P importPartFromFile: importableObjects: {}\n".format(len(importableObjects))) if len(importableObjects) == 1: DebugMsg(A2P_DEBUG_3,"a2p importPartFromFile: first and only file: {}\n".format(importableObjects[0])) else: DebugMsg(A2P_DEBUG_3,"a2p importPartFromFile: importableObjects:\n{}\n".format(importableObjects)) if len(importableObjects) > 1: subAssemblyImport = True ''' #------------------------------------------- # create new object #------------------------------------------- if importToCache: partName = 'CachedObject_' + str(objectCache.len()) newObj = doc.addObject("Part::FeaturePython", partName) newObj.Label = partName else: partName = a2plib.findUnusedObjectName(importDoc.Label, document=doc) partLabel = a2plib.findUnusedObjectLabel(importDoc.Label, document=doc) if PYVERSION < 3: newObj = doc.addObject("Part::FeaturePython", partName.encode('utf-8')) else: newObj = doc.addObject( "Part::FeaturePython", str(partName.encode('utf-8'))) # works on Python 3.6.5 newObj.Label = partLabel newObj.Proxy = Proxy_muxAssemblyObj() newObj.ViewObject.Proxy = ImportedPartViewProviderProxy() newObj.addProperty("App::PropertyString", "a2p_Version", "importPart").a2p_Version = A2P_VERSION assemblyPath = os.path.normpath(os.path.split(doc.FileName)[0]) absPath = os.path.normpath(filename) if getRelativePathesEnabled(): if platform.system() == "Windows": prefix = '.\\' else: prefix = './' relativePath = prefix + os.path.relpath(absPath, assemblyPath) newObj.addProperty("App::PropertyFile", "sourceFile", "importPart").sourceFile = relativePath else: newObj.addProperty("App::PropertyFile", "sourceFile", "importPart").sourceFile = absPath newObj.addProperty("App::PropertyStringList", "muxInfo", "importPart") newObj.addProperty("App::PropertyFloat", "timeLastImport", "importPart") newObj.setEditorMode("timeLastImport", 1) newObj.timeLastImport = os.path.getmtime(filename) newObj.addProperty("App::PropertyBool", "fixedPosition", "importPart") newObj.fixedPosition = not any( [i.fixedPosition for i in doc.Objects if hasattr(i, 'fixedPosition')]) newObj.addProperty("App::PropertyBool", "subassemblyImport", "importPart").subassemblyImport = subAssemblyImport newObj.setEditorMode("subassemblyImport", 1) newObj.addProperty("App::PropertyBool", "updateColors", "importPart").updateColors = True if subAssemblyImport: #if False: #newObj.muxInfo, newObj.Shape, newObj.ViewObject.DiffuseColor = muxObjectsWithKeys(importableObjects, withColor=True) newObj.muxInfo, newObj.Shape, newObj.ViewObject.DiffuseColor = muxAssemblyWithTopoNames( importDoc, withColor=True) else: # TopoMapper manages import of non A2p-Files. It generates the shapes and appropriate topo names... newObj.muxInfo, newObj.Shape, newObj.ViewObject.DiffuseColor = topoMapper.createTopoNames( withColor=True) doc.recompute() if importToCache: objectCache.add(filename, newObj) if not importDocIsOpen: FreeCAD.closeDocument(importDoc.Name) return newObj