For anyone looking for an Explode CAD option in C#, check out CADability on GitHub:
https://github.com/SOFAgh/CADability
There is a patch needed in the ExportStep.cs file you need to make:
- In the
WriteDefinition
method, you need to trim the s parameter if it already has a semi-color (the ACIS kernel throws an error when an empty entity is present in a STEP file):
public int WriteDefinition(string s)
{
int nr = currentItem;
++currentItem;
stream.WriteLine("#" + nr.ToString() + "=" + s.Trim(new chare] {';'}) + ";");
return nr;
}
Next, in your actual ZOS-API application, you will need to inherent the CADability.Project class to access the protected class:
class MyProject : Project {}
And then finally, you need to read in the CAD part, place each GeoObject in a separate Project and save each project:
// CAD file to be exploded
string cad = @"C:\Users\Michael\Documents\Zemax\Objects\CAD Files\CAD.stp";
string dir = Path.GetDirectoryName(cad);
// import the CAD part & convert to a GeoObject list
var importStep = new ImportStep();
var objectList = importStep.Read(cad);
// create a new export step class (make sure to patch WriteDefinition method or else this won't work with OpticStudio)
ExportStep exportStep = new ExportStep();
// loop through each child component and save as an individual STEP file
for (int i = 0; i < objectList.Count; i++)
{
// create a new project (need to inherit CADability.Project since this is a protected class)
var pr = new MyProject();
// create a new model and add the imported CAD objects to the model
var model = new Model();
model.Add(objectListei]);
// add the model to the project
pr.AddModel(model);
pr.SetActiveModel(0);
// increment filename to be saved
string fn = Path.Combine(dir, Path.GetFileNameWithoutExtension(cad) + "_" + i.ToString("D2") + ".stp");
exportStep.WriteToFile(fn, pr);
}
Hope this helps anyone in the future trying to explode CAD parts before actually doing something with them in OpticStudio.
Thank you @MichaelH for sharing this. I have moved your code to the API category of the forum.
I checked the documentation and didn’t find anything more, so I’ll add this into the “API implementation” feature request.
Thank you!