Convert STEP file type to STL - step

I want to convert a STEP file into an STL file format using Python. I have looked online and it looks like the best option is to either use FreeCAD or OpenCascade (OCC). However, I am a beginner and do not know where to start from. I did some search online and found this out (a code to convert STEP to OBJ file).
Are there any python examples from FreeCAD (based on OCC) to convert STEP files to STL? Where should I start?

Here's a quick bit of code to start out:
import FreeCAD
import Part
import Mesh
shape = Part.Shape()
shape.read('my_shape.step')
doc = App.newDocument('Doc')
pf = doc.addObject("Part::Feature","MyShape")
pf.Shape = shape
Mesh.export([pf], 'my_shape.stl')
FreeCAD uses python extensively for user-facing functions. Basically, anything you do through the UI is done with python.
So it's useful to open up the UI, open up the Python console, and then do a function manually. You can often just copy the python directly from the console and edited it to serve your needs.

Related

How to import all files at once pointed out by Dart Analysis?

I just put some of my code from a/b.dart to a/b1.dart file and now I started getting lot of errors on importing.
Is there any command or any other fix to import all a/b1.dart file in these files instead of manually opening each file and importing one by one.
I understand that a function or a property can be defined in more than two files and Dart can't make the right choice but if a function or property is defined in just one place, I think there must be some way to import it except searching for a/b.dart and replacing it with a/b.dart + a/b1.dart and then optimizing all imports.
As much as I am aware, Plugins/Extensions for your specific IDE (for dart) can be found that will help you with this problem.
I would recommend using dartdev tools - dartfix

music21 getElementsByClass not showing any output for class stream.Voice

I am struggling to understand why the below code is throwing an error when it ran seamlessly about a year back. The code snippet is from a popular Coursera course. Does the Music21 package has some recent changes around stream.Voice?
data_fn = 'data/original_metheny.mid'
midi_data = converter.parse(data_fn)
melody_stream = midi_data[5] # For Metheny piece, Melody is Part #5.
melody1, melody2 = melody_stream.getElementsByClass(stream.Voice)
The error thrown is ValueError: not enough values to unpack (expected 2, got 0), which means there is no output for stream.Voice class when previously there were outputs for the same data (midi file). melody_stream.getElementsByClass('Measure') does show outputs.
Can you guide how to debug this?
Yes, one of the improvements in music21 v.7 is that files imported from MIDI now have a similar representation to files imported from MusicXML and other formats. Specifically, Parts now have Measures, which may or may not have Voices, rather than Parts directly containing Voices. Code should not depend on finding Voices directly contained in Parts, which is what this example was doing.
Instead, use this code to find all the measure-voices:
melody_stream.recurse().getElementsByClass(stream.Voice)
Or, equivalently, use the shortcut syntax in v.7:
melody_stream[stream.Voice]
Or, if you don't want the measures at all, call flatten() or chordify() depending on your use case.
What worked for me was downgrading music21 package to a version older than 7.x. So if you already have a newer version of music21 package installed, remove it using pip uninstall music21, Then install the 6.7.0 version using pip install music21==6.7.0.

.NET methods in python (conversion from matlab code) - Delsys EMGWorks

I have a script in Matlab, which accesses a DLL and allows me to utilize the methods to import and analyse data programmatically. However, I would like to convert it to python. I have looked at using pythonnet, but I cannot get it to work. Can anyone suggest a way of replicating this behaviour in python?
Ths example is specific to Delsys and their EMGWorks software.
%locate HPF DLL within EMGworks install folder
path = ['C:\Program Files (x86)\pathtoDLL\HPF.dll'];
%make HPF assembly visible to MATLAB
NET.addAssembly(path);
%locate target HPF file
curFile = 'C:\TestFiles\MyTestFile.hpf';
%construct HPF reader
myHPFreader = HPF.HPFReader(curFile);
%invoke “GetAllSampleRates” method on HPF reader object “myHPFreader”
mySampleRates = myHPFreader.GetAllSampleRates;
Thanks Tim!
I made one small change to the great example you provided!
channel_names = emg_file.GetAllChannelNames()
The emg_file change matched the variable.
The key it seems is knowing the structure of the dll so you can instantiate the class. I looked at the Matlab docs to work this out.
1. Install Python.net
pip install pythonnet
2. Import package and make package available to Python
import clr
clr.AddReference('C:\Program Files (x86)\Delsys, Inc\EMGworks\Matlab Conversion Library \HPF.dll')
3. Instantiate class object
This was initially confusing for me. But it seems that the name of the DLL will be the name of the package. In my case the .dll is HPF.dll so my package name is HPF.
from HPF import HPFReader
emg_file = HPFReader(<filename>)
4. Utilise methods from table in matlab docs to get file info and print it out
channel_names = data.GetAllChannelNames()
for channel in channel_names:
print(channel)

Pycharm: Not Finding Pika Library (in path)

I spent 4 hours, on something simple, trying to figure out why pycharm did not find my pika library when running from inside development environment. The answer became obvious once found but for all you who are suffering from this simple issue try this:
Pycharm -> Run -> Configurations
Uncheck
Add content roots to PYTHONPATH
Add source roots to PYTHONPATH
Run/Debug Configurations
These settings should not result in you not finding the library in your PATH.
It's possible you have files in your project which mirror the names of the library or are otherwise interfering with resolution of the import name. You really should try to fix this issue right here, or you may find yourself having to debug even stranger problems after you send the code along to someone else.
Let's say that you're trying to run :
>>> import foo
This will look for foo.py, or a folder named foo containing __init.py__ in your PYTHONPATH.
If your own code also contains foo.py (or a folder named foo containing __init.py__), python will import your own module instead of the site package you're actually trying to import.
This may seemingly work without error, but if you were instead to do :
>>> from foo import fooclass
This class does not exists in your library, and therefore you're going to get an ImportError.
Similarly, if you did :
>>> import foo
>>> c = foo.fooclass()
You should get an AttributeError
Adding your source roots to PYTHONPATH is a fairly common requirement, and something you may need if your project grows beyond a few files. Not being able to do that can result in some really laborious workarounds in the future.

Export from OpenCascade, import into OpenSceneGraph

We have a modeling tool which uses OCC, and a 3d editor using OSG. What I want to do is, export the model from the first tool and import into the second tool. I have been searching the web for days, but I can't find a solution.
Three things can solve my problem:
An exporter for OCC to export into OSG supported formats (.ive, .osg, and many more),
An importer for OSG to import from OCC supported formats (.stp, .step, .igs, .iges, .brp, .brep ),
A converter tool for converting between two formats, one format supported by OCC and one format supported by OSG.
Has anybody done this before, or know of anything that can help?
I am trying to avoid writing a custom exporter for OCC.
I found a solution. OpenCascade has an import/export example, which can export VRML files without texture support. Some modifications on the import export code and some modifications on other parts (where the OCC model is represented by VRML classes) was enough to successfully export my model to a VRML file. Then i built the VRML plugin for OpenSceneGraph and successfully imported the model.
CADExchanger (OCC based) does a pretty good job converting between BRep and other formats (STEP, IGES, STL, VRML...)
Why don't you have a look at pythonocc.org.
I'm assuming OSG takes meshes?
Load the STEP / IGES file in (python)OCC, grab its mesh, push the verts / indices to OSG.
Would that work?