Exemple #1
0
def convert(source, dest):
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(source)

    if status == IFSelect_RetDone:
        i = 1
        ok = False
        number_of_roots = step_reader.NbRootsForTransfer()

        while i <= number_of_roots and not ok:
            ok = step_reader.TransferRoot(i)
            i += 1

        if (not ok):
            return {
                'error': 'Failed to find a suitable root for the STEP file'
            }

        shape = step_reader.Shape(1)
        output = os.path.abspath(dest)
        stl_ascii = False
        stl_writer = StlAPI_Writer()
        stl_writer.SetASCIIMode(stl_ascii)
        stl_writer.Write(shape, output)
        print "STL FILE: %s" % output

    else:
        print "Error, can't read file: %s" % './demo.stp'
Exemple #2
0
def main(argv):
  step_reader = STEPControl_Reader()
  status = step_reader.ReadFile(argv[0])

  if status == IFSelect_RetDone:  # check status
      failsonly = False
      step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
      step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)

      number_of_roots = step_reader.NbRootsForTransfer()

      ok = False
      i = 1

      while not ok and i <= number_of_roots:
        ok = step_reader.TransferRoot(i)
        i += 1

      _nbs = step_reader.NbShapes()
      aResShape = step_reader.Shape(1)
  else:
      print("Error: can't read file.")
      sys.exit(0)

  display, start_display, add_menu, add_function_to_menu = init_display()
  display.DisplayShape(aResShape, update=True)
  start_display()
def analyze_file(filename):
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(str(filename))
    result = None

    if status == IFSelect_RetDone:  # check status
        number_of_roots = step_reader.NbRootsForTransfer()
        ok = False
        i = 1

        while i <= number_of_roots and not ok:
            ok = step_reader.TransferRoot(i)
            i += 1

        if (not ok):
            return {
                'error': 'Failed to find a suitable root for the STEP file'
            }

        number_of_shapes = step_reader.NbShapes()

        if (number_of_shapes > 1):
            return {'error': 'Cannot handle more than one shape in a file'}

        aResShape = step_reader.Shape(1)

        # bounding box
        bbox = Bnd_Box()
        deflection = 0.01
        BRepMesh_IncrementalMesh(aResShape, deflection)

        brepbndlib_Add(aResShape, bbox)
        xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()

        bounding_box = calculate_bnd_box(bbox)

        result = {
            'x0': bounding_box['x_min'],
            'x1': bounding_box['x_max'],
            'y0': bounding_box['y_min'],
            'y1': bounding_box['y_max'],
            'z0': bounding_box['z_min'],
            'z1': bounding_box['z_max'],
            'length': bounding_box['x_length'],
            'width': bounding_box['z_length'],
            'height': bounding_box['y_length'],
            'volume': calculate_volume(aResShape)
        }

    else:
        result = {'error': 'Cannot read file'}

    return result
def get_shape_from_file(filename):
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(str(filename))

    if status == IFSelect_RetDone:
        number_of_roots = step_reader.NbRootsForTransfer()
    else:
        raise ConversionError("Could not read {}".format(filename))

    i = 1
    ok = False
    while not ok and i <= number_of_roots:
        ok = step_reader.TransferRoot(i)
        i += 1

    return ok and step_reader.Shape(1)
Exemple #5
0
def importStep(fileName):
    """
        Accepts a file name and loads the STEP file into a cadquery shape
        :param fileName: The path and name of the STEP file to be imported
    """
    # Now read and return the shape
    reader = STEPControl_Reader()
    readStatus = reader.ReadFile(fileName)
    if readStatus != OCC.IFSelect.IFSelect_RetDone:
        raise ValueError("STEP File could not be loaded")
    for i in range(reader.NbRootsForTransfer()):
        reader.TransferRoot(i + 1)

    occ_shapes = []
    for i in range(reader.NbShapes()):
        occ_shapes.append(reader.Shape(i + 1))

    # Make sure that we extract all the solids
    solids = []
    for shape in occ_shapes:
        solids.append(Shape.cast(shape))

    return cadquery.Workplane("XY").newObject(solids)
Exemple #6
0
def analyze_file(filename):
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(filename)
    result = None

    if status == IFSelect_RetDone:  # check status
        number_of_roots = step_reader.NbRootsForTransfer()
        ok = False
        i = 1

        while i <= number_of_roots and not ok:
            ok = step_reader.TransferRoot(i)
            i += 1

        if (not ok):
            return {
                'error': 'Failed to find a suitable root for the STEP file'
            }

        number_of_shapes = step_reader.NbShapes()

        if (number_of_shapes > 1):
            return {'error': 'Cannot handle more than one shape in a file'}

        aResShape = step_reader.Shape(1)

        # Units
        length = TColStd_SequenceOfAsciiString()
        angles = TColStd_SequenceOfAsciiString()
        solid_angles = TColStd_SequenceOfAsciiString()
        step_reader.FileUnits(length, angles, solid_angles)

        # bounding box
        bbox = Bnd_Box()
        deflection = 0.01
        BRepMesh_IncrementalMesh(aResShape, deflection)

        brepbndlib_Add(aResShape, bbox)
        xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()

        bounding_box = calculate_bnd_box(bbox)

        bounding_cylinder = calculate_bounding_cylinder(
            aResShape, bounding_box)

        result = {
            'bounding_box_volume': bounding_box['volume'],
            'bounding_box_x_length': bounding_box['x_length'],
            'bounding_box_y_length': bounding_box['y_length'],
            'bounding_box_z_length': bounding_box['z_length'],
            'mesh_volume': calculate_volume(aResShape),
            'mesh_surface_area': None,
            'cylinder_volume': bounding_cylinder['cylinder_volume'],
            'cylinder_diameter': bounding_cylinder['radius'] * 2,
            'cylinder_length': bounding_cylinder['height'],
            'convex_hull_volume': None,
            'euler_number': None,
            'units': length.First().ToCString().lower()
        }

    else:
        result = {'error': 'Cannot read file'}

    return result