找回密码
 注册
Simdroid-非首页
查看: 68|回复: 3

[二次开发] Sample NX Open Java program : export all bodies to stl

[复制链接]
发表于 2009-7-26 13:39:40 | 显示全部楼层 |阅读模式 来自 河北廊坊
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;
        }
}
发表于 2009-7-27 11:02:28 | 显示全部楼层 来自 辽宁沈阳
Simdroid开发平台
朋友,能不能把这个程序具体地注释一下呢,在下是新手,十分渴望!
另外有没有关于这方面的资料呢,万分感激!
回复 不支持

使用道具 举报

 楼主| 发表于 2009-7-29 20:38:27 | 显示全部楼层 来自 河北廊坊
我会再贴一些
回复 不支持

使用道具 举报

发表于 2009-7-30 10:22:21 | 显示全部楼层 来自 辽宁沈阳
期待中·················
才看到你其他的贴,看来你对ug二次开发的语言都有研究啊!
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-6-26 20:24 , Processed in 0.037933 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表