- 积分
- 10
- 注册时间
- 2007-10-5
- 仿真币
-
- 最后登录
- 1970-1-1
|
import nxopen.*;
import nxopen.uf.*;
import java.rmi.RemoteException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.ArrayList;
public class export_all_bodies_to_stl
{
public static void main(String[] args) throws NXException, Exception
{
ListingWindow lw = null;
UFSession ufs = null;
try
{
Session s = (Session)SessionFactory.get("Session");
ufs = (UFSession)SessionFactory.get("UFSession");
lw = s.listingWindow();
lw.open();
Part dispPart = s.parts().display();
BodyCollection bodies = dispPart.bodies();
BodyCollection.Iterator it = bodies.iterator();
// Open the STL binary file
UFVariant fileHandle = ufs.std().openBinaryStlFile("C:\\temp\\export_test.stl",
false,
"Created by NXOpen Java");
// Export all bodies with current WCS
while( it.hasNext() )
{
Body bd = (Body) it.next();
if( bd.isSolidBody() )
{
lw.writeLine("Solid Body " + bd.tagValue() );
ufs.std().putSolidInStlFile(
fileHandle,
Tag.NULL,
bd.tag(),
0, 100, 0.01);
}
else if( bd.isSheetBody() )
{
Face[] faces = bd.getFaces();
lw.writeLine("Sheet Body " + bd.tagValue() + " with " + faces.length + " faces.");
nxopen.Tag faceTags[] = MakeTagArrayFromObjArray(faces);
for (int i=0; i<faces.length; i++)
faceTags = ufs.modlFeatures().extractFace(faceTags, 0);
ufs.std().putSheetsInStlFile(
fileHandle,
Tag.NULL,
faces.length,
faceTags,
0, 100, 0.01, 0.1);
for (int i=0; i<faces.length; i++)
ufs.obj().deleteObject(faceTags);
}
}
// Close the file
ufs.std().closeStlFile(fileHandle);
}
catch (NXException ex)
{
if (!lw.isOpen()) lw.open();
lw.writeLine("Error code: " + ex.errorCode());
lw.writeLine("Error text: " +
ufs.UF().getFailMessage(ex.errorCode()));
}
catch (Exception ex)
{
if(ufs != null)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("Caught exception " + ex );
ex.printStackTrace(pw);
lw.writeLine("\nFailed");
lw.writeLine("\n"+ex.getMessage());
lw.writeLine("\n"+sw.getBuffer().toString());
}
}
}
public static nxopen.Tag[] MakeTagArrayFromObjArray(TaggedObject[] objects) throws NXException, Exception
{
// Suppress [unchecked] warning as of Java 1.5.0
ArrayList<nxopen.Tag> alist = new ArrayList<nxopen.Tag>();
// but Java 1.4.2 needs to be called without type
//ArrayList alist = new ArrayList();
for (int i=0; i<objects.length; i++)
{
alist.add(objects.tag());
}
int count = alist.size();
nxopen.Tag tagList[] = new nxopen.Tag[count];
for( int i=0; i<count; i++)
{
tagList = (nxopen.Tag) alist.get(i);
}
return tagList;
}
public static int getUnloadOption()
{
return BaseSession.LibraryUnloadOption.IMMEDIATELY;
}
} |
|