def save2SVG (things, outputFilename): """exports things to svg file """ if type(things) is not list: things = [things] outList = [] tmpParts = [] for thing in things: tmpPart = mydoc.addObject("Part::Feature") tmpParts.append(tmpPart) tmpParts[-1].Shape = thing importSVG.export(tmpParts, outputFilename) for obj in mydoc.Objects: mydoc.removeObject(obj.Name) return
def export(self, p, result, label, data_hash, config): # export the plate to different file formats log.info("Exporting %s layer for %s" % (label, data_hash)) # draw the part so we can export it Part.show(p.val().wrapped) doc = FreeCAD.ActiveDocument # export the drawing into different formats pwd_len = len(config['app']['pwd']) # the absolute part of the working directory (aka - outside the web space) result['exports'][label] = [] if 'js' in result['formats']: with open("%s/%s_%s.js" % (config['app']['export'], label, data_hash), "w") as f: cadquery.exporters.exportShape(p, 'TJS', f) result['exports'][label].append({'name':'js', 'url':'%s/%s_%s.js' % (config['app']['export'][pwd_len:], label, data_hash)}) log.info("Exported 'JS'") if 'brp' in result['formats']: Part.export(doc.Objects, "%s/%s_%s.brp" % (config['app']['export'], label, data_hash)) result['exports'][label].append({'name':'brp', 'url':'%s/%s_%s.brp' % (config['app']['export'][pwd_len:], label, data_hash)}) log.info("Exported 'BRP'") if 'stp' in result['formats']: Part.export(doc.Objects, "%s/%s_%s.stp" % (config['app']['export'], label, data_hash)) result['exports'][label].append({'name':'stp', 'url':'%s/%s_%s.stp' % (config['app']['export'][pwd_len:], label, data_hash)}) log.info("Exported 'STP'") if 'stl' in result['formats']: Mesh.export(doc.Objects, "%s/%s_%s.stl" % (config['app']['export'], label, data_hash)) result['exports'][label].append({'name':'stl', 'url':'%s/%s_%s.stl' % (config['app']['export'][pwd_len:], label, data_hash)}) log.info("Exported 'STL'") if 'dxf' in result['formats']: importDXF.export(doc.Objects, "%s/%s_%s.dxf" % (config['app']['export'], label, data_hash)) result['exports'][label].append({'name':'dxf', 'url':'%s/%s_%s.dxf' % (config['app']['export'][pwd_len:], label, data_hash)}) log.info("Exported 'DXF'") if 'svg' in result['formats']: importSVG.export(doc.Objects, "%s/%s_%s.svg" % (config['app']['export'], label, data_hash)) result['exports'][label].append({'name':'svg', 'url':'%s/%s_%s.svg' % (config['app']['export'][pwd_len:], label, data_hash)}) log.info("Exported 'SVG'") if 'json' in result['formats'] and label == SWITCH_LAYER: with open("%s/%s_%s.json" % (config['app']['export'], label, data_hash), 'w') as json_file: json_file.write(repr(self)) result['exports'][label].append({'name':'json', 'url':'%s/%s_%s.json' % (config['app']['export'][pwd_len:], label, data_hash)}) log.info("Exported 'JSON'") # remove all the documents from the view before we move on for o in doc.Objects: doc.removeObject(o.Label)
# List all the DXF files to read files = [f for f in os.listdir(input_path) if isfile(join(input_path, f))] obj_with_content = [] obj_with_content_fat = [] coordinates = [] print "---------- Creation des fichiers svg ----------" for f in files: current_filename = splitext(f)[0] # Import the DXF with the FreeCad library importDXF.open(join(input_path, f)) doc = FreeCAD.ActiveDocument importSVG.export( doc.Objects, join(output_path, 'all_objects_' + current_filename + '.svg')) #Display a progress bar widgets = [ f + ' :', progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage() ] pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(FreeCAD.ActiveDocument.Objects)) pbar.start() for i, ob in enumerate(doc.Objects): pbar.update(i)
def export(self, p, result, label, data_hash, config): # export the plate to different file formats log.info("Exporting %s layer for %s" % (label, data_hash)) # draw the part so we can export it Part.show(p.val().wrapped) doc = FreeCAD.ActiveDocument # export the drawing into different formats # the absolute part of the working directory (aka - outside the web # space) pwd_len = len(config['app']['pwd']) result['exports'][label] = [] if 'js' in result['formats']: with open( "%s/%s_%s.js" % (config['app']['export'], label, data_hash), "w") as f: cadquery.exporters.exportShape(p, 'TJS', f) result['exports'][label].append({ 'name': 'js', 'url': '%s/%s_%s.js' % (config['app']['export'][pwd_len:], label, data_hash) }) log.info("Exported 'JS'") if 'brp' in result['formats']: Part.export( doc.Objects, "%s/%s_%s.brp" % (config['app']['export'], label, data_hash)) result['exports'][label].append({ 'name': 'brp', 'url': '%s/%s_%s.brp' % (config['app']['export'][pwd_len:], label, data_hash) }) log.info("Exported 'BRP'") if 'stp' in result['formats']: Part.export( doc.Objects, "%s/%s_%s.stp" % (config['app']['export'], label, data_hash)) result['exports'][label].append({ 'name': 'stp', 'url': '%s/%s_%s.stp' % (config['app']['export'][pwd_len:], label, data_hash) }) log.info("Exported 'STP'") if 'stl' in result['formats']: Mesh.export( doc.Objects, "%s/%s_%s.stl" % (config['app']['export'], label, data_hash)) result['exports'][label].append({ 'name': 'stl', 'url': '%s/%s_%s.stl' % (config['app']['export'][pwd_len:], label, data_hash) }) log.info("Exported 'STL'") if 'dxf' in result['formats']: importDXF.export( doc.Objects, "%s/%s_%s.dxf" % (config['app']['export'], label, data_hash)) result['exports'][label].append({ 'name': 'dxf', 'url': '%s/%s_%s.dxf' % (config['app']['export'][pwd_len:], label, data_hash) }) log.info("Exported 'DXF'") if 'svg' in result['formats']: importSVG.export( doc.Objects, "%s/%s_%s.svg" % (config['app']['export'], label, data_hash)) result['exports'][label].append({ 'name': 'svg', 'url': '%s/%s_%s.svg' % (config['app']['export'][pwd_len:], label, data_hash) }) log.info("Exported 'SVG'") if 'json' in result['formats'] and label == SWITCH_LAYER: with open( "%s/%s_%s.json" % (config['app']['export'], label, data_hash), 'w') as json_file: json_file.write(repr(self)) result['exports'][label].append({ 'name': 'json', 'url': '%s/%s_%s.json' % (config['app']['export'][pwd_len:], label, data_hash) }) log.info("Exported 'JSON'") # remove all the documents from the view before we move on for o in doc.Objects: doc.removeObject(o.Label)
# Craft some geometry vec = cad.FreeCAD.Vector doc = cad.FreeCAD.newDocument('template') doc.addObject('Spreadsheet::Sheet','modelParams') sketch = doc.addObject('Sketcher::SketchObject', 'Sketch') ProfileLib.RegularPolygon.makeRegularPolygon('Sketch', 6, vec(10,10,0), vec(120,120,0), False) sketch.addConstraint(cad.sketchUtils.Sketcher.Constraint('Distance',2,1.)) sketch.setExpression('Constraints['+str(len(sketch.Constraints)-1)+']', u'modelParams.thickness_wire') # Set up geometry task tag_al = qtf.SweepTag('Al thickness') tag_wire = qtf.SweepTag('Wire diameter') freecad_dict = { 'document': doc, 'params': {'thickness_al': tag_al, 'thickness_wire': tag_wire} } geo_task = GeoFreeCAD(options=freecad_dict) sweeps = [ {tag_al: 1., tag_wire: val} for val in np.arange(2,10,2) ] # ~ sweeps = [ {tag_al: val1, tag_wire: val2} # ~ for val1 in np.arange(2,10,2) for val2 in [5.,6.] ] # ~ sweep_man = qtf.SweepManager.construct_cartesian_product({}) qtf.SweepManager(sweeps).run(geo_task) # Investigate results if not os.path.exists('tmp'): os.mkdir('tmp') for res in geo_task.result: # ~ res.saveAs("example_geogen_"+str(res.modelParams.d1)+".fcstd") draft = cad.sketchUtils.Draft.makeShape2DView(res.Sketch) import importSVG importSVG.export([draft],u"tmp/example_geogen_"+str(res.modelParams.thickness_wire)+".svg")