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