UG的二次開發有兩套系統,一套叫Open,一套叫NXOpen。Open主要是造型方面的功能,NXOpen比較全面。Open原來支援的是C/C++,.net的NXOpen.UF命名空間支援。NXOpen支援C++和.net等。
Open系統,支援C的原來叫UFun,或者API,用的人最多。後來出現了Open C++。但是Open C++支援編輯等屬性行為,不能建立。所以,一般是通過API建立特徵,比如實體,通過C++的類查詢和修改。
NXOpen系統,是完全物件導向的,所以可以建立和修改特徵。當然,NXOpen幾乎支援UG所有的功能。
Open
NXOpen
C
UFun(API);面向過程開發;主要支援造型功能
C++
Open C++類庫;物件導向開發;部分支援造型功能,沒有建立特徵的功能等,需要使用UFun
通過NXOpen命名空間支援,需要包含相應標頭檔。
.net
通過NXOpen.UF命名空間封裝了UFun來實現。
通過NXOpen命名空間支援,需要引用相應的程式集。
所以,目前開來,如果使用C/C++方式,可以使用Open C和C++結合的方式,利用C來建立特徵,使用C++來管理。如果使用.net可以直接使用NXOpen。對於不熟悉NXOpen的人可以按照Open C的知識上手NXOpen.UF。
下面將通過各個例子說明上述系統的使用,因為.net平台是通用的,我只舉了C#的例子,VB等也是一樣的的。而java我不懂,見諒了。
一、Open C
1、遍曆的例子
#include <uf_object_types.h>
#include <uf_part.h>
#include <uf_obj.h>
#include <uf_modl.h>
#include <string>
#include <sstream>
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 <uf.h>
#include <uf_ui.h>
#include <uf_exit.h>
#include <uf_modl.h>
//下面是程式片段
/* 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 <ug_typed.hxx>
#include <ug_part.hxx>
#include <ug_body.hxx>
#include <ug_string.hxx>
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 <ug_body.hxx>
#include <ug_iterator.hxx>
#include <ug_string.hxx>
//下面是程式片段
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 <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
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 <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/ListingWindow.hxx>
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);
}