def createSrcDoc(fileName): import a import FreeCAD myname = "alib.createSrcDoc()" a.log(myname, "started") docList = FreeCAD.listDocuments() for doc in docList: if docList[doc].FileName == fileName: a.log(a.myname(), "doc allready open") return docList[doc] msg = "doc is not open. Opening the doc" a.log(a.myname(), msg) return FreeCAD.openDocument(fileName)
def objUsable(obj): import a myname = "alib.objUsable()" a.log(myname, "started") if obj != None: try: #check if obj allready deleted obj.__dict__ except ReferenceError as e: a.log(a.myname(), "No, it is deleted") return False a.log(a.myname(), "Yes, it is real " + repr(obj)) return True a.log(a.myname(), "No, it is None") return False
def getFirstSelectionFrom(doc): import FreeCADGui import a myname = "alib.getFirstSelectionFrom()" a.log(myname, "started") obj = None a.log(a.myname(), "doc = " + repr(doc)) sel = FreeCADGui.Selection.getSelection(doc.Name) a.log(a.myname(), "sel = " + repr(len(sel))) if len(sel) > 0: obj = sel[0] a.log(a.myname(), "selected item: " + repr(obj)) return obj a.log(a.myname(), "function error: no object selected") raise ValueError('getFirstSelection error: obj = None')
def createDestDoc(srcdoc=None): import FreeCAD import FreeCADGui import a myname = "alib.createDestDoc()" a.log(myname, "started") if objUsable(a.destDoc): a.log(myname, "DestDoc allready exists") return a.destDoc docs = FreeCAD.listDocuments() a.log(myname, "DestDocList " + repr(len(docs))) if len(docs) == 0: a.log(myname, "DestDocList == 0, creating new destdoc ") a.destDoc = FreeCAD.newDocument() return a.destDoc if len(docs) == 1: if docs.values()[0] == srcdoc: a.log(myname, "the only opened doc is srcdoc, creating new destdoc ") a.destDoc = FreeCAD.newDocument() return a.destDoc for doc in docs: #find acceptable active doc to use as destdoc a.log(myname, "DestDocList iteration ") if docs[doc] != srcdoc: a.log(myname, "iter, docs[doc] != srcdoc ") if docs[doc] == a.initialActiveDoc: a.log( myname, "iter, docs[doc] is a.initialActiveDoc, setting as destdoc " + repr(docs[doc].Name) + " " + repr(docs[doc])) a.destDoc = a.initialActiveDoc return a.destDoc else: a.log(myname, "iter, docList[doc] is not active") if a.destDoc == srcdoc: a.log(myname, "function error: a.destDoc == srcdoc ") raise ValueError('a.destDoc == srcdoc') if a.destDoc == None: a.log(myname, "function error: a.destDoc == None") raise ValueError(a.myname() + ' error: a.destDoc == None')
def createDestPart(desDoc, sPart): import a myname = "alib.createDestPart()" a.log(myname, "started") import FreeCAD destPart = desDoc.addObject("Part::FeaturePython") a.log(a.myname(), "destPart = " + repr(destPart)) destPart.Label = sPart.Label destPart.addProperty("App::PropertyString", "objType", a.label).objType = a.name destPart.addProperty("App::PropertyFile", "sourceFile", a.label).sourceFile = sPart.Document.FileName destPart.addProperty("App::PropertyString", "srcName", a.label).srcName = sPart.Name destPart.addProperty("App::PropertyString", "srcLabel", a.label).srcLabel = sPart.Label destPart.Shape = sPart.Shape.copy() destPart.ViewObject.ShapeColor = sPart.ViewObject.ShapeColor destPart.ViewObject.DiffuseColor = sPart.ViewObject.DiffuseColor #destPart.ViewObject.DisplayMode = sPart.ViewObject.DisplayMode #crash in FreeCAD 0.16 destPart.ViewObject.DrawStyle = sPart.ViewObject.DrawStyle #destPart.ViewObject.ShapeMaterial = sPart.ViewObject.ShapeMaterial #disables diffuse colors destPart.ViewObject.Lighting = sPart.ViewObject.Lighting destPart.ViewObject.LineColor = sPart.ViewObject.LineColor destPart.ViewObject.LineMaterial = sPart.ViewObject.LineMaterial destPart.ViewObject.LineWidth = sPart.ViewObject.LineWidth destPart.ViewObject.Transparency = sPart.ViewObject.Transparency destPart.ViewObject.PointColor = sPart.ViewObject.PointColor destPart.ViewObject.PointMaterial = sPart.ViewObject.PointMaterial destPart.ViewObject.PointSize = sPart.ViewObject.PointSize # just set ViewObject.Proxy to something different from None #(this assignment is needed to run an internal notification) destPart.ViewObject.Proxy = 0 #end creating destPart a.destPart = destPart return destPart