def __init__(self, *args, **kwargs): global object_index object_index[type(self).__name__] += 1 self.name = "%s%d" % (type(self).__name__, object_index[type(self).__name__]) if 'filename' in kwargs: self.filename = os.path.expanduser(kwargs['filename']) elif 'url' in kwargs: f = tempfile.NamedTemporaryFile(delete=True) self.filename = f.name data = urllib2.urlopen(kwargs['url']).read() f.write(data) f.flush() f.seek(0) if 'thickness' in kwargs: self.thickness = kwargs['thickness'] # noinspection PyUnusedLocal doc_name = 'svg' + ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(6) ]) # doc_name = 'svg_%s' % os.path.basename(self.filename) doc = FreeCAD.newDocument(doc_name) if self.filename and doc: importSVG.insert(self.filename, doc_name) solids = [] for obj in doc.Objects: shape = obj.Shape wire = Part.Wire(shape.Edges) face = Part.Face(wire) solids.append(face.extrude(Base.Vector(0, 0, self.thickness))) self.object = Part.makeCompound(solids) FreeCAD.closeDocument(doc_name) FreeCAD.setActiveDocument(SimpleSolidPy.root_window.doc.Name) self._width = self.object.BoundBox.XLength self._length = self.object.BoundBox.YLength self._height = self.object.BoundBox.ZLength # SimpleSolidPy.root_window.centerView() self.attachments = { 'center': Attachment(self), 'top': TopAttachment(self), 'bottom': BottomAttachment(self) } self.show() # self.doc_object.Placement = FreeCAD.Placement(FreeCAD.Vector(0,0,0),FreeCAD.Rotation(0,0,0,1)) SimpleSolidPy.root_window.doc.recompute() SimpleSolidPy.root_window.loop_once()
def extrudeSVG(filename, thickness): doc = App.ActiveDocument objcnt = len(doc.Objects) # Import SVG importSVG.insert(filename, doc.Name) # returns None Gui.SendMsgToActiveView("ViewFit") # Get latest object in document: for i in range(objcnt, len(doc.Objects)): SVG = doc.Objects[i] if 'Shape' not in dir(SVG): print "Skipping object {0}, it has no Shape".format(SVG.Name) continue if SVG.Shape.ShapeType not in ('Wire', 'Edge'): print "Skipping shape {0} of type {1}".format( SVG.Name, SVG.Shape.ShapeType) continue # Gui.ActiveDocument.activeObject() # Gui.ActiveDocument.path3400 # All objects: App.ActiveDocument.Objects[0] etc # Try to create face, hide SVG try: tmp = Part.Face(Part.Wire(Part.__sortEdges__(SVG.Shape.Edges))) if tmp.isNull(): raise RuntimeError('Failed to create face') SVGFace = doc.addObject('Part::Feature', 'SVGFace') SVGFace.Shape = tmp del tmp except Exception: print "Can't create face from part:", SVG.Name finally: SVG.ViewObject.Visibility = False # Extrude face SVGExtrude = doc.addObject("Part::Extrusion", "SVGExtrude") SVGExtrude.Base = SVGFace SVGExtrude.Dir = (0, 0, thickness) SVGExtrude.Solid = (True) SVGExtrude.TaperAngle = (0) SVGFace.ViewObject.Visibility = False doc.recompute()
def extractInlays(filename, baseName): # Deferred Imports to speedup Workbench activation import importSVG # Save Working doc workingDoc = App.ActiveDocument # Import SVG File name = utils.randomString(16) doc = App.newDocument(name, 'Importing', True) importSVG.insert(filename, name) # Extract inlays = {} for obj in doc.Objects: extractInlay(obj, inlays) if len(inlays) == 0: ui.errorDialog('The SVG File does not contain any inlay path') return # Build inlays for fret, inlay in inlays.items(): inlay.buildShape() # Restore Active Doc App.setActiveDocument(workingDoc.Name) App.closeDocument(doc.Name) # Create parts for fret, inlay in inlays.items(): inlay.createPart(baseName) # Recalculate App.ActiveDocument.getObject( MarzInstrument_Name).Internal_InlayImport = int(time.time())
sys.path.append(FREECADPATH) import FreeCAD print("FreeCAD.Version:", FreeCAD.Version()) #FreeCAD.Console.PrintMessage("Hello from PrintMessage!\n") # avoid using this method because it is not printed in the FreeCAD GUI import Part from FreeCAD import Base print("dbg111: start building the 3D part") my_tmp_doc = FreeCAD.newDocument( "doc_blabla" ) # you can create implicitly the document "doc_blabla" by using it! import importSVG importSVG.insert("Aphex_Twin_Logo.svg", "doc_blabla") ## Two methods are possible: # 1. Emulating the GUI control # or # 2. Using the Part module as soon as possible. ### 1. GUI control emulation #my_tmp_doc.addObject("Part::Extrusion","Extrude") ##FreeCAD.getDocument("doc_blabla").addObject("Part::Extrusion","Extrude") # alternative syntax #my_tmp_doc.Extrude.Base = my_tmp_doc.path2992 # path2992 is the ID written in the svg file ##my_tmp_doc.Extrude.Base = FreeCAD.getDocument("doc_blabla").path2992 # alternative syntax #my_tmp_doc.Extrude.Dir = (0,0,2) #my_tmp_doc.Extrude.Solid = (True) #my_tmp_doc.Extrude.TaperAngle = (0) #my_tmp_doc.recompute() # create the shape
# Save third group variable as layer number layer_number = re.search(pattern, file_name).group(4) # Save fourth group variable as z depth z_depth = float(re.search(pattern, file_name).group(5))/1000 print(modifier, layer_type, layer_number, z_depth) doc = FreeCAD.newDocument("flow") print("Number of objects in the document: " + str(len(doc.Objects))) # Import the SVG file importSVG.insert(filename=file_name, docname=doc.Name) # Get the number of objects in the document num_objects = len(doc.Objects) print("Number of objects in the document: " + str(num_objects)) print(doc.Objects) sketch = doc.addObject('Sketcher::SketchObject','CombinedSketch') print(doc.Objects) #turn the inported objects into sketches i = 0 for obj in doc.Objects: print(type(obj)) print(i)
def extractCustomShape(filename, baseName, requireContour=True, requireMidline=True): # Deferred Imports to speedup Workbench activation import importSVG import Part # Contour implies midline requireMidline = requireMidline or requireContour # Save Working doc workingDoc = App.ActiveDocument # Import SVG File name = utils.randomString(16) doc = App.newDocument(name, 'Importing', True) importSVG.insert(filename, name) # Find contour and midline by id contour = None midline = None transition = None pockets = [] for obj in doc.Objects: if obj.Name == 'contour': contour = obj elif obj.Name == 'midline': midline = obj elif obj.Name == 'transition': transition = obj else: extractPocket(obj, pockets) if not contour and requireContour: ui.errorDialog( 'The SVG File does not contain any contour path. Make sure you have a path with id=contour' ) return if not midline and requireMidline: ui.errorDialog( 'The SVG File does not contain any midline path. Make sure you have a path with id=midline' ) return # Load contour wcontour = None if requireContour: # Find contour and midline intersection (d, vs, es) = midline.Shape.distToShape(contour.Shape) anchor = vs[0][0] # Intersection tolerance if d > 0.0000001: ui.errorDialog( 'contour path and midline path must intersect where the neck will be anchored' ) return # Copy Shapes and Upgrade Paths to Wires wcontour = Part.Wire(contour.Shape.copy().Edges) wcontour.translate(-anchor) anchor = anchor or Vector(0, 0, 0) # If no reference anchor # Find transition Segment wtransition = None if transition: (d, vs, es) = transition.Shape.distToShape(contour.Shape) if d < 1e-5 and len(vs) > 1: wtransition = Part.Wire( Part.Shape([Part.LineSegment(vs[0][0], vs[1][0])])) wtransition.translate(-anchor) # Build pockets compound solids = [] for pocket in pockets: wire = Part.Wire(pocket.edges) wire.translate(-anchor) wire.translate(Vector(0, 0, -pocket.start)) solid = Part.Face(wire).extrude(Vector(0, 0, -pocket.depth)) solids.append((pocket, solid)) # Restore Active Doc App.setActiveDocument(workingDoc.Name) App.closeDocument(doc.Name) # --------------------------------------------- # Build pockets def merge(base, s): if base is None: return s else: return base.fuse(s) if solids: comp = None compT = None compB = None for p, s in solids: if p.target == 't': compT = merge(compT, s) elif p.target == 'b': compB = merge(compB, s) else: comp = merge(comp, s) if comp: ui.addOrUpdatePart(comp, baseName + '_Pockets', 'Pockets', visibility=False, group=ui.UIGroup_Imports) if compT: ui.addOrUpdatePart(compT, baseName + '_Pockets_Top', 'Pockets', visibility=False, group=ui.UIGroup_Imports) if compB: ui.addOrUpdatePart(compB, baseName + '_Pockets_Back', 'Pockets', visibility=False, group=ui.UIGroup_Imports) # Add contour to document if wcontour: ui.addOrUpdatePart(wcontour, baseName + '_Contour', 'Contour', visibility=False, group=ui.UIGroup_Imports) if wtransition: ui.addOrUpdatePart(wtransition, baseName + '_Transition', 'Transition', visibility=False, group=ui.UIGroup_Imports) # Recalculate if baseName == 'Marz_Headstock': App.ActiveDocument.getObject( MarzInstrument_Name).Internal_HeadstockImport = int(time.time()) elif baseName == 'Marz_Body': App.ActiveDocument.getObject( MarzInstrument_Name).Internal_BodyImport = int(time.time())
print("freecad_gui:", freecad_gui) if not(freecad_gui): print("dbg101: add FREECADPATH to sys.path") sys.path.append(FREECADPATH) import FreeCAD print("FreeCAD.Version:", FreeCAD.Version()) import Part from FreeCAD import Base print("dbg111: start building the 3D part") my_tmp_doc = FreeCAD.newDocument("doc_0") import importSVG importSVG.insert(input_svg_file,"doc_0") my_solids = [] for obj in my_tmp_doc.Objects: my_svg_shape = obj.Shape my_svg_wire = Part.Wire(my_svg_shape.Edges) my_svg_face = Part.Face(my_svg_wire) # straight linear extrusion my_solids.append(my_svg_face.extrude(Base.Vector(0,0,10))) my_compound = Part.makeCompound(my_solids) ## view and export your 3D part output_stl_file="allpaths.stl" Part.show(my_compound) # works only with FreeCAD GUI, ignore otherwise my_compound.exportStl(output_stl_file)
sys.path.append(FREECADPATH) import FreeCAD print("FreeCAD.Version:", FreeCAD.Version()) #FreeCAD.Console.PrintMessage("Hello from PrintMessage!\n") # avoid using this method because it is not printed in the FreeCAD GUI import Part from FreeCAD import Base print("dbg111: start building the 3D part") my_tmp_doc = FreeCAD.newDocument( "doc_blabla" ) # you can create implicitly the document "doc_blabla" by using it! import importSVG importSVG.insert("benzene_sin_text_connected.svg", "doc_blabla") ## Two methods are possible: # 1. Emulating the GUI control # or # 2. Using the Part module as soon as possible. ### 1. GUI control emulation # my_tmp_doc.addObject("Part::Extrusion","Extrude") ##FreeCAD.getDocument("doc_blabla").addObject("Part::Extrusion","Extrude") # alternative syntax # my_tmp_doc.Extrude.Base = my_tmp_doc.path2295 # path2992 is the ID written in the svg file # my_tmp_doc.Extrude.Base = FreeCAD.getDocument("doc_blabla").path2295 # alternative syntax # my_tmp_doc.Extrude.Dir = (0,0,2) # my_tmp_doc.Extrude.Solid = (True) # my_tmp_doc.Extrude.TaperAngle = (0) # my_tmp_doc.recompute() # create the shape
# Save third group variable as layer number substrate_number = re.search(pattern, file_name).group(3) # Save the fourth group variable as the feature layer depth z_depth = SUBSTRATE_Z_DIM # Save fourth group variable as z depth z_depth = float(re.search(pattern, file_name).group(4)) / 1000 print(modifier, layer_type, substrate_number, z_depth) doc = FreeCAD.newDocument(file_name.replace(".svg", "")) # Import the SVG file importSVG.insert(filename="/home/krishna/CIDAR/3duf-server/svg2stl/" + file_name, docname=doc.Name) # Get the number of objects in the document num_objects = len(doc.Objects) face_objects = [] # # Get the imported object for i in range(len(doc.Objects)): SVG = doc.Objects[i] if "Shape" not in dir(SVG): print("I'm not sure what this part is with the shape ?") continue if SVG.Shape.ShapeType not in ("Wire", "Edge"): print("Skipping shape {0} of type {1}".format(SVG.Name,
# Save third group variable as layer number substrate_number = re.search(pattern, file_name).group(3) # Save the fourth group variable as the feature layer depth z_depth = SUBSTRATE_Z_DIM # Save fourth group variable as z depth z_depth = float(re.search(pattern, file_name).group(4)) / 1000 print(modifier, layer_type, substrate_number, z_depth) doc = FreeCAD.newDocument(file_name.replace(".svg", "")) # Import the SVG file importSVG.insert(filename=file_name, docname=doc.Name) # Get the number of objects in the document num_objects = len(doc.Objects) extrude_objects = [] # # Get the imported object for i in range(len(doc.Objects)): SVG = doc.Objects[i] if "Shape" not in dir(SVG): print("I'm not sure what this part is with the shape ?") continue if SVG.Shape.ShapeType not in ("Wire", "Edge"): print("Skipping shape {0} of type {1}".format(SVG.Name, SVG.Shape.ShapeType)) continue