环境: Win7 x64,VS2015, NX12.0.2.9
目标: NX12图纸模块,需要获取某个图纸上的全部注释。
注:图纸和注释仿佛是两个体系,没有找到两者直接的关系,于是自己写了一个,仅供参考!!!
代码:
test.cpp
#include <NXOpen/Annotations_Annotation.hxx>
#include <NXOpen/Annotations_AnnotationManager.hxx>
#include <NXOpen/Annotations_BreakSettingsBuilder.hxx>
#include <NXOpen/Annotations_DimensionStyleBuilder.hxx>
#include <NXOpen/Annotations_DisplayStyleBuilder.hxx>
#include <NXOpen/Annotations_DraftingNoteBuilder.hxx>
#include <NXOpen/Annotations_ForeshorteningSymbolBuilder.hxx>
#include <NXOpen/Annotations_ForeshorteningSymbolSettingsBuilder.hxx>
#include <NXOpen/Annotations_FrameBarElementStyleBuilder.hxx>
#include <NXOpen/Annotations_FrameBarStyleBuilder.hxx>
#include <NXOpen/Annotations_HatchStyleBuilder.hxx>
#include <NXOpen/Annotations_HoleCalloutSettingsBuilder.hxx>
#include <NXOpen/Annotations_LeaderBuilder.hxx>
#include <NXOpen/Annotations_LeaderData.hxx>
#include <NXOpen/Annotations_LeaderDataList.hxx>
#include <NXOpen/Annotations_LetteringStyleBuilder.hxx>
#include <NXOpen/Annotations_LineArrowStyleBuilder.hxx>
#include <NXOpen/Annotations_OrdinateStyleBuilder.hxx>
#include <NXOpen/Annotations_OriginBuilder.hxx>
#include <NXOpen/Annotations_PlaneBuilder.hxx>
#include <NXOpen/Annotations_RadialStyleBuilder.hxx>
#include <NXOpen/Annotations_SingleSidedDisplayBuilder.hxx>
#include <NXOpen/Annotations_StyleBuilder.hxx>
#include <NXOpen/Annotations_SymbolStyleBuilder.hxx>
#include <NXOpen/Annotations_TextWithEditControlsBuilder.hxx>
#include <NXOpen/Annotations_TextWithSymbolsBuilder.hxx>
#include <NXOpen/Annotations_UnitsStyleBuilder.hxx>
#include <NXOpen/BasePart.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/Builder.hxx>
#include <NXOpen/DisplayableObject.hxx>
#include <NXOpen/Drawings_DraftingDrawingSheet.hxx>
#include <NXOpen/Drawings_DraftingDrawingSheetCollection.hxx>
#include <NXOpen/Drawings_DraftingView.hxx>
#include <NXOpen/Drawings_DrawingSheet.hxx>
#include <NXOpen/Drawings_SelectDraftingView.hxx>
#include <NXOpen/Expression.hxx>
#include <NXOpen/ExpressionCollection.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_EditWithRollbackManager.hxx>
#include <NXOpen/Features_Feature.hxx>
#include <NXOpen/Features_FeatureBuilder.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/GeometricUtilities_BooleanOperation.hxx>
#include <NXOpen/MenuBar_ContextMenuProperties.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/ObjectList.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Point.hxx>
#include <NXOpen/SelectDisplayableObject.hxx>
#include <NXOpen/SelectDisplayableObjectList.hxx>
#include <NXOpen/SelectObject.hxx>
#include <NXOpen/Selection.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/TaggedObject.hxx>
#include <NXOpen/Unit.hxx>
#include <NXOpen/Update.hxx>
#include <NXOpen/Xform.hxx>
#include <NXOpen\Annotations_Note.hxx>
#include <NXOpen\Annotations_NoteCollection.hxx>
#include <NXOpen\View.hxx>
#include <uf.h>
#include <uf_defs.h>
#include <uf_disp.h>
#include <uf_draw.h>
#include <uf_drf.h>
#include <uf_obj.h>
#include <uf_part.h>
#include <uf_view.h>
#include <vector>
bool IsNote(tag_t obj)
{
int type = 0;
int subtype = 0;
UF_OBJ_ask_type_and_subtype(obj, &type, &subtype);
if (type == UF_drafting_entity_type && subtype == UF_draft_note_subtype)
{
return true;
}
return false;
}
std::vector<tag_t> GetViewVisibleObjects(tag_t view)
{
std::vector<tag_t> objects;
int count = 0;
tag_t* firstObjPtr = NULL;
int countClip = 0;
tag_t* firstObjClipPtr = NULL;
UF_VIEW_ask_visible_objects(view, &count, &firstObjPtr, &countClip, &firstObjClipPtr);
for (int i = 0; i < count; ++i)
{
objects.push_back(*firstObjPtr);
if (i != count - 1)
{
++firstObjPtr;
}
}
return objects;
}
//the view must created in sheet,not trans by sheet
std::vector<tag_t> GetNotesOnView(tag_t view)
{
std::vector<tag_t> notes;
int num = 0;
tag_p_t firstNote;
UF_DRAW_ask_view_notes(view, &num, &firstNote);
for (int i = 0; i < num; ++i)
{
notes.push_back(firstNote[i]);
}
UF_free(firstNote);
return notes;
}
//sheet trans to view
tag_t GetViewBySheet(tag_t sheet)
{
tag_t view = NULL_TAG;
UF_DRAW_ask_view_of_drawing(sheet, &view);
return view;
}
std::vector<tag_t> GetViewsOnSheet(tag_t sheet)
{
std::vector<tag_t> views;
int numViews = 0;
tag_p_t tmpViews = NULL;
UF_DRAW_ask_views(sheet, &numViews, &tmpViews);
for (int i = 0; i < numViews; ++i)
{
tag_t view = tmpViews[i];
views.push_back((view));
}
UF_free(tmpViews);
return views;
}
std::vector<tag_t> GetWorkPartNotes()
{
std::vector<tag_t> notes;
NXOpen::Annotations::NoteCollection* collection = NXOpen::Session::GetSession()->Parts()->Work()->Notes();
for (NXOpen::Annotations::NoteCollection::iterator it = collection->begin(); it != collection->end(); ++it)
{
notes.push_back((*it)->Tag());
}
return notes;
}
tag_t GetActiveSheet()
{
NXOpen::Drawings::DraftingDrawingSheetCollection * collection = NXOpen::Session::GetSession()->Parts()->Work()->DraftingDrawingSheets();
for (NXOpen::Drawings::DraftingDrawingSheetCollection::iterator it = collection->begin(); it != collection->end(); ++it)
{
NXOpen::Drawings::DraftingDrawingSheet* tmpSheet = *it;
if (tmpSheet->IsActiveForSketching())
{
return tmpSheet->Tag();
}
}
return NULL_TAG;
}
void OpenSheet(tag_t sheet)
{
NXOpen::Drawings::DraftingDrawingSheet* tmpSheet = dynamic_cast<NXOpen::Drawings::DraftingDrawingSheet*>(COMN_NX::ToNXOpenObject(sheet));
if (tmpSheet != NULL)
{
tmpSheet->Open();
}
}
std::vector<tag_t> GetSheetNotes(tag_t sheet)
{
std::vector<tag_t> transViewNotes;
tag_t transView = GetViewBySheet(sheet);
if (transView != NULL_TAG)
{
std::vector<tag_t> transViewObjs = GetViewVisibleObjects(transView);
for (int k = 0; k < transViewObjs.size(); ++k)
{
if (IsNote(transViewObjs[k]))
{
transViewNotes.push_back(transViewObjs[k]);
}
}
}
return transViewNotes;
}
std::vector<tag_t> GetWorkPartSheets()
{
std::vector<tag_t> sheets;
NXOpen::Drawings::DraftingDrawingSheetCollection * collection = NXOpen::Session::GetSession()->Parts()->Work()->DraftingDrawingSheets();
for (NXOpen::Drawings::DraftingDrawingSheetCollection::iterator it = collection->begin(); it != collection->end(); ++it)
{
sheets.push_back((*it)->Tag());
}
return sheets;
}
void TestSheet()
{
tag_t originActiveSheet = GetActiveSheet();
std::vector<tag_t> workPartSheets = GetWorkPartSheets();
for (int i = 0; i < workPartSheets.size(); ++i)
{
//must open sheet otherwise GetViewVisibleObjects(tag_t view) will fail
OpenSheet(workPartSheets[i]);
//sheetViews is created by user
std::vector<tag_t> sheetViews = GetViewsOnSheet(workPartSheets[i]);
for (int j = 0; j < sheetViews.size(); ++j)
{
std::vector<tag_t> viewObjs = GetViewVisibleObjects(sheetViews[j]);
std::vector<tag_t> viewNotes = GetNotesOnView(sheetViews[j]);
}
//this view can get all notes in sheet
std::vector<tag_t> sheetNotes = GetSheetNotes(workPartSheets[i]);
}
OpenSheet(originActiveSheet);
}