Solved

Explode CAD via ZOS-API

  • 24 April 2023
  • 2 replies
  • 85 views

Userlevel 6
Badge +2

Maybe I’m missing something, but is there a way to take a CAD Part as an assembly and run the NCE Explode CAD Part via the ZOS-API?  The only “explode” results I get when searching the Help are for the Explode? cell which is only a {get;} letting you know if the object was exploded.

If this doesn’t exist yet in the API, then Zemax take note, this is another “almost there” situation.  For sequential, all the RunTool_ implementations for the LDE are a single line in the ZOS-API source code so getting a NCE.RunTool_ExplodeCAD should be less than 1 day effort for the dev team.  Please complete all the “tools” that live in the toolbar of each editor.

icon

Best answer by MichaelH 24 April 2023, 22:47

View original

2 replies

Userlevel 6
Badge +2

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 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 char[] {';'}) + ";");
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(objectList[i]);

// 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.

Userlevel 6
Badge +2

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!

Reply