Beispiel #1
0
 def utility_create_scad(self, scadCode, name):
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = temp_dir + os.path.sep + name + ".scad"
         f = open(filename,"w+")
         f.write(scadCode)
         f.close()
         return importCSG.open(filename)
Beispiel #2
0
 def utility_create_scad(self, scadCode, name):
     filename = self.temp_dir.name + os.path.sep + name + ".scad"
     print(f"Creating {filename}")
     f = open(filename, "w+")
     f.write(scadCode)
     f.close()
     return importCSG.open(filename)
Beispiel #3
0
def convert_openscad_to_step(scad_filename,
                             step_filename,
                             objname='difference'):

    # NOTES:
    # * Works with ubuntu 18.04 (python 2.7.17 and freecad 0.16)
    # * Needs rework for ubuntu 20.04 (python 3.8.2 and freecad 0.18.4)

    # TODO: automatically determine the top level object (objname)

    part_name = os.path.basename(scad_filename)
    part_name = part_name.replace(".scad", "")

    importCSG.open(scad_filename)
    FreeCAD.setActiveDocument(part_name)
    part = FreeCAD.getDocument(part_name).getObject(objname)
    Part.export([part], step_filename)
Beispiel #4
0
 def test_import_sphere(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = temp_dir + os.path.sep + "sphere.scad"
         f = open(filename,"w+")
         f.write("sphere(10.0);")
         f.close()
         doc = importCSG.open(filename)
         sphere = doc.getObject("sphere")
         self.assertTrue (sphere is not None)
         self.assertTrue (sphere.Radius == 10.0)
         FreeCAD.closeDocument(doc.Name)
Beispiel #5
0
 def test_import_polygon_path(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = temp_dir + os.path.sep + "polygon_path.scad"
         f = open(filename,"w+")
         f.write("polygon([[0,0],[100,0],[130,50],[30,50]], paths=[[0,1,2,3]]);")
         f.close()
         doc = importCSG.open(filename)
         wire = doc.ActiveObject # With paths, the polygon gets created as a wire...
         self.assertTrue (wire is not None)
         self.assertAlmostEqual (wire.Shape.Area, 5000.0)
         FreeCAD.closeDocument(doc.Name)
Beispiel #6
0
 def test_import_polygon_nopath(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = temp_dir + os.path.sep + "polygon_nopath.scad"
         f = open(filename,"w+")
         f.write("polygon(points=[[0,0],[100,0],[130,50],[30,50]]);")
         f.close()
         doc = importCSG.open(filename)
         polygon = doc.getObject("polygon")
         self.assertTrue (polygon is not None)
         self.assertAlmostEqual (polygon.Shape.Area, 5000.0)
         FreeCAD.closeDocument(doc.Name)
Beispiel #7
0
 def test_import_cylinder(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = temp_dir + os.path.sep + "cylinder.scad"
         f = open(filename,"w+")
         f.write("cylinder(50.0,d=10.0);")
         f.close()
         doc = importCSG.open(filename)
         cylinder = doc.getObject("cylinder")
         self.assertTrue (cylinder is not None)
         self.assertTrue (cylinder.Radius == 5.0)
         self.assertTrue (cylinder.Height == 50.0)
         FreeCAD.closeDocument(doc.Name)
Beispiel #8
0
 def test_import_square(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = temp_dir + os.path.sep + "square.scad"
         f = open(filename,"w+")
         f.write("square([1.0,2.0]);")
         f.close()
         doc = importCSG.open(filename)
         square = doc.getObject("square")
         self.assertTrue (square is not None)
         self.assertTrue (square.Length == 1.0)
         self.assertTrue (square.Width == 2.0)
         FreeCAD.closeDocument(doc.Name)
Beispiel #9
0
 def test_import_text(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = temp_dir + os.path.sep + "text.scad"
         f = open(filename,"w+")
         f.write("text(\"X\");") # Keep it short to keep the test fast-ish
         f.close()
         try:
             doc = importCSG.open(filename)
             text = doc.getObject("text")
             self.assertTrue (text is not None)
             FreeCAD.closeDocument(doc.Name)
         except Exception:
             pass # We may not have the DXF importer available
Beispiel #10
0
    def test_open_csg(self):
        testfile = join(self.test_dir, "CSG.csg")
        doc = importCSG.open(testfile)

        # Doc should now contain three solids: a union, an intersection, and a difference
        union = doc.getObject("union")
        intersection = doc.getObject("intersection")
        difference = doc.getObject("difference")

        self.assertTrue (union is not None)
        self.assertTrue (intersection is not None)
        self.assertTrue (difference is not None)

        FreeCAD.closeDocument("CSG")
Beispiel #11
0
    def test_import_polyhedron(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            filename = temp_dir + os.pathsep + "polyhedron.scad"
            f = open(filename, "w+")
            f.write("""
polyhedron(
  points=[ [10,10,0],[10,-10,0],[-10,-10,0],[-10,10,0], // the four points at base
           [0,0,10]  ],                                 // the apex point 
  faces=[ [0,1,4],[1,2,4],[2,3,4],[3,0,4],              // each triangle side
              [1,0,3],[2,1,3] ]                         // two triangles for square base
 );
""")
            f.close()
            doc = importCSG.open(filename)
            polyhedron = doc.ActiveObject  # With paths, the polygon gets created as a wire...
            self.assertTrue(polyhedron is not None)
            self.assertAlmostEqual(polyhedron.Shape.Volume, 1333.3333, 4)
            FreeCAD.closeDocument(doc.Name)
Beispiel #12
0
if not len(sys.argv) == 3:
    print("Usage: %s file.csg file.step" % sys.argv[0])
    sys.exit(1)

file_input = sys.argv[1]
file_output = sys.argv[2]

print("Converting %s to %s" % (file_input, file_output))

sys.path.append("/Applications/FreeCAD.app/Contents/Resources/lib")
import FreeCAD
import importCSG
import Part

d = importCSG.open(file_input)

o = []

# Select parent node
for i in d.Objects:
    if not i.InList:
        o.append(i)
print(len(o))
quit()
# Perform the Fusion
App.activeDocument().addObject("Part::MultiFuse", "Fusion")
App.activeDocument().Fusion.Shapes = o
App.ActiveDocument.recompute()

# Then the Refine
Beispiel #13
0
# -*- coding: utf-8 -*-
# scad-to-step.py
# FreeCAD macro to import *.scad and export *.step
# Brian K. White - [email protected]
# Requires:
# * foo.scad file, where "foo" is passed via environment variable MODEL=foo
# * freecad with openscad workbench installed and configured:
#    * path to openscad binary
#    * use view provider in tree view
#    * use multimatrix feature

import Part
import importCSG
import os

MODEL = os.environ['MODEL']

d = importCSG.open(MODEL + '.scad')

s = Part.getShape(d.TopologicalSortedObjects[0])

n = s.removeSplitter()

n.exportStep(MODEL + '.step')