谈谈UG二次开发的Open和NXOpen

UG的二次开发有两套系统,一套叫Open,一套叫NXOpenOpen主要是造型方面的功能,NXOpen比较全面。Open原来支持的是C/C++.netNXOpen.UF命名空间支持。NXOpen支持C++.net等。

Open系统,支持C的原来叫UFun,或者API,用的人最多。后来出现了Open C++。但是Open C++支持编辑等属性行为,不能创建。所以,一般是通过API创建特征,比如实体,通过C++的类查询和修改。

NXOpen系统,是完全面向对象的,所以可以创建和修改特征。当然,NXOpen几乎支持UG所有的功能。

 

Open

NXOpen

C

UFunAPI);面向过程开发;主要支持造型功能

 

C++

Open C++类库;面向对象开发;部分支持造型功能,没有创建特征的功能等,需要使用UFun

通过NXOpen命名空间支持,需要包含相应头文件。

.net

通过NXOpen.UF命名空间包装了UFun来实现。

通过NXOpen命名空间支持,需要引用相应的程序集。

 

所以,目前开来,如果使用C/C++方式,可以使用Open CC++结合的方式,利用C来创建特征,使用C++来管理。如果使用.net可以直接使用NXOpen。对于不熟悉NXOpen的人可以按照Open C的知识上手NXOpen.UF

 

下面将通过各个例子说明上述系统的使用,因为.net平台是通用的,我只举了C#的例子,VB等也是一样的的。而java我不懂,见谅了。

 

一、Open C

1、遍历的例子

#include

#include

#include

#include

#include

#include

using std::string;

using std::stringstream;

 

//下面是程序片段

    UgSession session( true );

    try

    {

        /* TODO: Add your application code here */

         uf_list_p_t lpObj;

         UF_MODL_create_list(&lpObj);

 

         tag_t prt = UF_PART_ask_display_part();

         tag_t Next_tag=NULL_TAG;

         do

         {

              UF_OBJ_cycle_objs_in_part(prt,UF_solid_type,&Next_tag);

              if(Next_tag==NULL_TAG) break;

 

              int t,subtype;

            UF_OBJ_ask_type_and_subtype(Next_tag,&t,&subtype);

              if(subtype==UF_solid_body_subtype)

                   UF_MODL_put_list_item(lpObj,Next_tag);

         } while(1);

 

         logical is_open;

         UF_UI_is_listing_window_open(&is_open);

         if(!is_open) UF_UI_open_listing_window();

 

         int sum;

         UF_MODL_ask_list_count(lpObj,&sum);

         for (int i=0;i<sum;i++)

         {

              tag_t t;

              UF_MODL_ask_list_item(lpObj,i,&t);

 

              stringstream s;

              s<<(int)t;

              string str;

              str = s.str();

 

            UF_UI_write_listing_window(str.c_str());

              UF_UI_write_listing_window("/n");

         }

 

//       UF_UI_exit_listing_window();

         UF_MODL_delete_list(&lpObj);

    }

 

    /* Handle errors */

    catch ( const UgException &exception )

    {

        processException( exception );

}

 

2,创建block的例子

#include

#include

#include

#include

//下面是程序片段

 

   /* Initialize the API environment */

    if( UF_CALL(UF_initialize()) )

    {

        /* Failed to initialize */

        return;

    }

   

    /* TODO: Add your application code here */

     double corner[3] ={0,0,0};

     char* edge[3] = {"10","5","20"};

     tag_t tag;

     UF_MODL_create_block(UF_NULLSIGN,NULL_TAG,corner,edge,&tag);

 

    /* Terminate the API environment */

UF_CALL(UF_terminate());

 

二、Open C++

1、遍历的例子

#include

#include

#include

#include

using std::string;

//下面是程序片段

 

    UgSession session( true );

    try

    {

        /* TODO: Add your application code here */

         UgPart           *pWorkPart = UgSession::getWorkPart();

         UgTypedObject    *pObj;

 

         ostrstream buffer;

 

         for ( pObj = pWorkPart->iterateFirst ( );

              pObj;

              pObj = pWorkPart->iterateNext ( pObj ) )

         {

              std::string name = pObj->getName ( );

 

              UgBody *pBody = dynamic_cast<UgBody*>(pObj);

              if (pBody)

              {

                   buffer<<(int)pBody->getTag()<<"/n";

              }

         }

 

         UgInfoWindow::open();

         UgInfoWindow::write(string(buffer.str()));

         delete buffer.str();

    }

 

    /* Handle errors */

    catch ( const UgException &exception )

    {

        processException( exception );

}

 

2、通过模板搜索的例子

#include

#include

#include

//下面是程序片段

 

    UgSession session( true );

 

    try

    {

        /* TODO: Add your application code here */

         ostrstream buffer;

         // Construct an iterator for NX face objects

         UgIterator < UgBody > pObj;//workpart 可以通过其他方式指定

 

         //UgIterator < UgFace *> curFace;

         Loop through all faces

         //while ( !curFace.isFinished ( ) )

         //{

         //   // Get the name of the current face

         //   std::string faceName = (*(*curFace))->getName ( );

         //   curFace.findNext ( );

         //}

 

         // Loop through all faces

         while ( !pObj.isFinished ( ) )

         {

              // Get the name of the current face

              std::string faceName = (*pObj)->getName ( );

              buffer<<(int)(*pObj)->getTag()<<endl;

              pObj.findNext ( );

         }

        

         UgInfoWindow::open();

         UgInfoWindow::write( std::string( buffer.str() ));

         delete buffer.str();

        

    }

 

    /* Handle errors */

    catch ( const UgException &exception )

    {

        processException( exception );

}

 

 

三、NXOpen C++

1、创建block的例子

#include

#include

#include

#include

#include

#include

#include

#include

using namespace NXOpen;

//下面是程序片段

 

     NXOpen::Session *theSession = NXOpen::Session::GetSession();

 

    try

    {

        /* TODO: Add your application code here */

         Part* thePart = theSession->Parts()->Work();

         NXOpen::Features::Feature* block = NULL;

         NXOpen::Features::BlockFeatureBuilder* theBuilder = thePart->Features()->CreateBlockFeatureBuilder(block);

 

         NXOpen::Point3d basePoint(100, 100, 100);

         theBuilder->SetOriginAndLengths(basePoint, "100", "200", "300");

 

         //              NXOpen.Body theBody = null;

         //              theBuilder.SetBooleanOperationAndTarget(NXOpen.Features.Feature.BooleanType.Create, theBody);

 

         theBuilder->Commit();

         //theBuilder->CommitFeature();

 

         NXOpen::UI::GetUI()->NXMessageBox()->Show("", NXMessageBox::DialogType::DialogTypeInformation, "OK!");

     //   UI->GetUI()->NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");

 

    }

 

    /* Handle errors */

    catch ( const UgException &exception )

    {

        processException( exception );

}

 

2、遍历特征的例子

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace NXOpen;

//下面是程序片段

 

     NXOpen::Session *theSession = NXOpen::Session::GetSession();

 

     try

     {

         /* TODO: Add your application code here */

         Part* thePart = theSession->Parts()->Work();

         theSession->ListingWindow()->Open();

 

         NXOpen::Features::FeatureCollection::iterator i;

         for (i=thePart->Features()->begin();i!=thePart->Features()->end();i++)

         {

              theSession->ListingWindow()->WriteLine((*i)->Name()+"--"+(*i)->GetJournalIdentifier());

         }

 

         NXOpen::UI::GetUI()->NXMessageBox()->Show("", NXMessageBox::DialogType::DialogTypeInformation, "OK!");

         //   UI->GetUI()->NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");

 

     }

 

    /* Handle errors */

    catch ( const UgException &exception )

    {

        processException( exception );

}

 

 

四、NXOpen C#

1、创建blcok的例子

using NXOpen;

using NXOpen.Utilities;

using NXOpen.UF;

using NXOpenUI;

//下面是程序片段

 

            Session theSession = Session.GetSession();

 

            try

            {

                Part thePart = theSession.Parts.Work;

                NXOpen.Features.Feature block = null;

                NXOpen.Features.BlockFeatureBuilder theBuilder = thePart.Features.CreateBlockFeatureBuilder(block);

 

                NXOpen.Point3d basePoint = new Point3d( 100f , 100f , 100f );

                theBuilder.SetOriginAndLengths(basePoint, "100", "200", "300");

 

                theBuilder.Commit();

              //theBuilder.CommitFeature();

 

                UI.GetUI().NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");

            }

            catch(NXException ex)

            {

                UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);

            }

 

2、遍历特征的例子

using NXOpen;

using NXOpen.Utilities;

using NXOpen.UF;

using NXOpenUI;

//下面是程序片段

 

            Session theSession = Session.GetSession();

            theSession.ListingWindow.Open();

 

            try

            {

                Part thePart = theSession.Parts.Work;

 

                foreach (NXOpen.Features.Feature c in thePart.Features)

                {

                    //NXOpen.Features.Block t = c as NXOpen.Features.Block;

                    //if(t!=null)

                    //{

                    //    theSession.ListingWindow.WriteLine(t.ToString());

                    //}

                    theSession.ListingWindow.WriteLine(c.Name+"--"+c.ToString());

                }

               

                UI.GetUI().NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");

            }

            catch (NXException ex)

            {

                UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);

            }

 

五、NXOpen.UF

1、仿照Open C中的例1子实现

using NXOpen;

using NXOpen.Utilities;

using NXOpen.UF;

using NXOpenUI;

 

            NXOpen.UF.UFSession theUFSession = NXOpen.UF.UFSession.GetUFSession();

            try

            {

                double[] corner ={ 0, 0, 0 };

                string[] edge = { "10", "5", "20" };

                Tag tag;

 

                theUFSession.Modl.CreateBlock1(FeatureSigns.Nullsign, corner, edge, out tag);

            }

            catch (NXException ex)

            {

                UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);

            }

2、仿照Open C中的例子2实现

 

using NXOpen;

using NXOpen.Utilities;

using NXOpen.UF;

using NXOpenUI;

 

            NXOpen.UF.UFSession theUFSession = NXOpen.UF.UFSession.GetUFSession();

 

            try

            {

                Tag[] list=null;

               

                Tag thePart = theUFSession.Part.AskDisplayPart();

 

                theUFSession.Modl.CreateList(out list);

 

                Tag Next_tag=Tag.Null;

                do

                {

                    theUFSession.Obj.CycleObjsInPart(thePart,70/* UF_solid_type*/,ref Next_tag);

 

                    if (Next_tag == Tag.Null) break;

 

                    int t, subType;

                    theUFSession.Obj.AskTypeAndSubtype(Next_tag,out t, out subType);

                    if (subType == 0/*UF_solid_body_subtype*/)

                        theUFSession.Modl.PutListItem(list, Next_tag);

 

 

                } while (true);

 

                bool isOpen;

                theUFSession.Ui.IsListingWindowOpen(out isOpen);

                if (!isOpen) theUFSession.Ui.OpenListingWindow();

 

                int sum;

                theUFSession.Modl.AskListCount(list,out sum);

                for (int i = 0; i < sum;i++ )

                {

                    Tag t;

                    theUFSession.Modl.AskListItem(list, i, out t);

                    theUFSession.Ui.WriteListingWindow(t.ToString());

                }

 

                /*Treat all the arguments with the"Output to be freed " annotation as an output parameter.

                 * The system takes care of freeing memory.*/

 

 

            }

            catch (NXException ex)

            {

                UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);

            }

 

你可能感兴趣的:(UG二次开发/NX,Development)