UG/NX二次开发Siemens官方NXOPEN实例解析—3.3 TitleBlock(添加图纸Title)

前言

        通过程序自动添加图纸的Title,在我以为图纸的title一般会直接做到模板里,然后通过调用模板输出,官方之所以专门出了这样一个实例,我想是为了给自动出图纸做铺垫吧。

一、知识点提取

本实例实现了自动添加图纸Title,主要包括一下内容:

1、获取图纸信息和系统信息

2、创建title的表格

3、填入title的文本内容

二、效果图

三、源码分析

 1、源码所在目录

UGOPEN\SampleNXOpenApplications\C++\TitleBlock

2、获取图纸信息和系统信息

Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());
Part *displayPart(theSession->Parts()->Display());

//Getting Part Name into String
NXOpen::BlockStyler::PropertyList *partNameProps = partName->GetProperties();
NXOpen::NXString partName1 = partNameProps->GetString("WideValue");
delete partNameProps;

//Getting Author Name into String
NXOpen::BlockStyler::PropertyList *authNameProps = authorName->GetProperties();
NXOpen::NXString authorName1 = authNameProps->GetString("WideValue");
delete authNameProps;

//Getting Current Date into String
UF_system_info_t info;
UF_ask_system_info(&info);
			
//Now the info.date_buf contains date and time seperated be space
//from which date will be extracted
NXOpen::NXString currDate = info.date_buf;		
string date = currDate.GetText();
size_t pos1 = date.find_first_of(" ");
date = date.substr(0,pos1);
currDate = date;	

3、创建title的表格

// ----------------------------------------------
//   Menu: Insert->Annotation...
// ----------------------------------------------
Session::UndoMarkId markId1;
markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible,  "Create Annotation");
Annotations::LetteringPreferences *letteringPreferences1;
letteringPreferences1 = workPart->Annotations()->Preferences()->GetLetteringPreferences();
// ----------------------------------------------------------------------
//   For this example, we need the set the lettering preferences so the
//   Text will align correctly in the title block
// ----------------------------------------------------------------------
Annotations::Lettering annotations_Lettering1;
annotations_Lettering1.Size = 0.125;
annotations_Lettering1.CharacterSpaceFactor = 1;
annotations_Lettering1.AspectRatio = 1.0;
annotations_Lettering1.LineSpaceFactor = 1.0;
annotations_Lettering1.Cfw.Color = 2;
annotations_Lettering1.Cfw.Font = 1;
annotations_Lettering1.Cfw.Width = Annotations::LineWidthThin;
letteringPreferences1->SetGeneralText(annotations_Lettering1);
workPart->Annotations()->Preferences()->SetLetteringPreferences(letteringPreferences1);
Annotations::UserSymbolPreferences *userSymbolPreferences1;
userSymbolPreferences1 = theSession->Parts()->Work()->Annotations()->NewUserSymbolPreferences(Annotations::UserSymbolPreferences::SizeTypeScaleAspectRatio,1,1);
// -------------------------------------------------------------------------------------
//   We need to load in the custom sybmol of the simple title block that has three lines
// -------------------------------------------------------------------------------------
NXString name = theSession->Parts()->Work()->FullPath();
string fullpath = name.GetText();
size_t pos = fullpath.find_last_of("\\"); ;
fullpath = fullpath.substr(0,(pos+1));
name = fullpath;
name = name +"special.sbf";
theSession->Parts()->Work()->Annotations()->CurrentSbfFile() = name;
double *symbWidth= new double,*symbHgt = new double;
NXOpen::SymbolFont *symbolFont1 =  theSession->Parts()->Work()->Annotations()->LoadSymbolFontFromSbfFile("TITLE4  ",symbWidth,symbHgt);

4、填入title的文本内容

// ----------------------------------------------
//   This adds table into the location
// ----------------------------------------------
std::vector<NXString> stringArray1(1) ; 
stringArray1[0]= "<%TITLE4>";

NXOpen::Point3d point3d1(12.9219405594406, 2.3166958041958, 0);
Annotations::Note *note1 = theSession->Parts()->Work()->Annotations()->CreateNote(stringArray1,point3d1,AxisOrientationHorizontal,letteringPreferences1,userSymbolPreferences1);
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
// ----------------------------------------------------------
//   This adds in the note for the Part Name title entry
//   
//  We set up an association, to the title block note, so 
//  when the title block is moved the text will travel with it
//------------------------------------------------------------
stringArray1[0]= "<C3.250>Part Name<C>";
NXOpen::Point3d point3d2(0, 0, 0);
Annotations::Note *note2 = theSession->Parts()->Work()->Annotations()->CreateNote(stringArray1,point3d2,AxisOrientationHorizontal,letteringPreferences1,userSymbolPreferences1);
// ----------------------------------------------
//   Menu: Edit->Placement->Origin Tool
// ----------------------------------------------
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
Annotations::Annotation::AssociativeOriginData annotation_AssociativeOriginData2;
annotation_AssociativeOriginData2.OriginType = Annotations::AssociativeOriginTypeOffsetFromText;
View *nullView(NULL);
annotation_AssociativeOriginData2.View = nullView;
annotation_AssociativeOriginData2.ViewOfGeometry = nullView;
Point *nullPoint(NULL);
annotation_AssociativeOriginData2.PointOnGeometry = nullPoint;
annotation_AssociativeOriginData2.VertAnnotation = NULL;
annotation_AssociativeOriginData2.VertAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData2.HorizAnnotation = NULL;
annotation_AssociativeOriginData2.HorizAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData2.AlignedAnnotation = NULL;
annotation_AssociativeOriginData2.DimensionLine = 0;
annotation_AssociativeOriginData2.AssociatedView = nullView;
annotation_AssociativeOriginData2.AssociatedPoint = nullPoint;
annotation_AssociativeOriginData2.OffsetAnnotation = note1;
annotation_AssociativeOriginData2.OffsetAlignmentPosition = Annotations::AlignmentPositionMidCenter;
annotation_AssociativeOriginData2.XOffsetFactor = -70;
annotation_AssociativeOriginData2.YOffsetFactor = 8 ;
NXOpen::Point3d point3d3(2.54694055944056, 3.3166958041958, 0);
note2->SetAssociativeOrigin(annotation_AssociativeOriginData2,point3d3);
theSession->UpdateManager()->DoUpdate(markId1);
markId1 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Create Annotation");
// ----------------------------------------------------------
//   This adds in the note for the Author title entry
//   
//  We set up an association, to the title block note, so 
//  when the title block is moved the text will travel with it
//------------------------------------------------------------
stringArray1[0]= "<C3.250>Author<C>";
Annotations::Note *note3 = theSession->Parts()->Work()->Annotations()->CreateNote(stringArray1,point3d2,AxisOrientationHorizontal,letteringPreferences1,userSymbolPreferences1);
// ----------------------------------------------
//   Menu: Edit->Placement->Origin Tool
// ----------------------------------------------
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
Annotations::Annotation::AssociativeOriginData annotation_AssociativeOriginData3;
annotation_AssociativeOriginData3.OriginType = Annotations::AssociativeOriginTypeOffsetFromText;
annotation_AssociativeOriginData3.View = NULL;
annotation_AssociativeOriginData3.ViewOfGeometry = NULL;
annotation_AssociativeOriginData3.PointOnGeometry = NULL;
annotation_AssociativeOriginData3.VertAnnotation = NULL;
annotation_AssociativeOriginData3.VertAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData3.HorizAnnotation = NULL;
annotation_AssociativeOriginData3.HorizAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData3.AlignedAnnotation = NULL;
annotation_AssociativeOriginData3.DimensionLine = 0;
annotation_AssociativeOriginData3.AssociatedView = NULL;
annotation_AssociativeOriginData3.AssociatedPoint = NULL;
annotation_AssociativeOriginData3.OffsetAnnotation = note1;
annotation_AssociativeOriginData3.OffsetAlignmentPosition = Annotations::AlignmentPositionMidCenter;
annotation_AssociativeOriginData3.XOffsetFactor = -70;
annotation_AssociativeOriginData3.YOffsetFactor = 1 ;
NXOpen::Point3d point3d5(1.92194055944056, 2.4416958041958, 0);
note3->SetAssociativeOrigin(annotation_AssociativeOriginData3,point3d5);
theSession->UpdateManager()->DoUpdate(markId1);
markId1 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Create Annotation");
// ----------------------------------------------------------
//   This adds in the note for the Date title entry
//   
//  We set up an association, to the title block note, so 
//  when the title block is moved the text will travel with it
//------------------------------------------------------------
stringArray1[0]= "<C3.250>Revision Date<C>";
Annotations::Note *note4 = theSession->Parts()->Work()->Annotations()->CreateNote(stringArray1,point3d2,AxisOrientationHorizontal,letteringPreferences1,userSymbolPreferences1);
// ----------------------------------------------
//   Menu: Edit->Placement->Origin Tool
// ----------------------------------------------
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
Annotations::Annotation::AssociativeOriginData annotation_AssociativeOriginData4;
annotation_AssociativeOriginData4.OriginType = Annotations::AssociativeOriginTypeOffsetFromText;
annotation_AssociativeOriginData4.View = NULL;
annotation_AssociativeOriginData4.ViewOfGeometry = NULL;
annotation_AssociativeOriginData4.PointOnGeometry = NULL;
annotation_AssociativeOriginData4.VertAnnotation = NULL;
annotation_AssociativeOriginData4.VertAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData4.HorizAnnotation = NULL;
annotation_AssociativeOriginData4.HorizAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData4.AlignedAnnotation = NULL;
annotation_AssociativeOriginData4.DimensionLine = 0;
annotation_AssociativeOriginData4.AssociatedView = NULL;
annotation_AssociativeOriginData4.AssociatedPoint = NULL;
annotation_AssociativeOriginData4.OffsetAnnotation = note1;
annotation_AssociativeOriginData4.OffsetAlignmentPosition = Annotations::AlignmentPositionMidCenter;
annotation_AssociativeOriginData4.XOffsetFactor = -70;
annotation_AssociativeOriginData4.YOffsetFactor = -7 ;
NXOpen::Point3d point3d7(3.42194055944056, 1.4416958041958, 0);
note4->SetAssociativeOrigin(annotation_AssociativeOriginData4,point3d7);
theSession->UpdateManager()->DoUpdate(markId1);
markId1 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Create Annotation");
// ----------------------------------------------------------
//   This adds in the note for the Part name
//   
//  We set up an association, to the title block note, so 
//  when the title block is moved the text will travel with it
//------------------------------------------------------------
stringArray1[0]=  "<C3.250>" + partName1 + "<C>";
Annotations::Note *note5 = theSession->Parts()->Work()->Annotations()->CreateNote(stringArray1,point3d2,AxisOrientationHorizontal,letteringPreferences1,userSymbolPreferences1);
// ----------------------------------------------
//   Menu: Edit->Placement->Origin Tool
// ----------------------------------------------
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
Annotations::Annotation::AssociativeOriginData annotation_AssociativeOriginData5;
annotation_AssociativeOriginData5.OriginType = Annotations::AssociativeOriginTypeOffsetFromText;
annotation_AssociativeOriginData5.View = NULL;
annotation_AssociativeOriginData5.ViewOfGeometry = NULL;
annotation_AssociativeOriginData5.PointOnGeometry = NULL;
annotation_AssociativeOriginData5.VertAnnotation = NULL;
annotation_AssociativeOriginData5.VertAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData5.HorizAnnotation = NULL;
annotation_AssociativeOriginData5.HorizAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData5.AlignedAnnotation = NULL;
annotation_AssociativeOriginData5.DimensionLine = 0;
annotation_AssociativeOriginData5.AssociatedView = NULL;
annotation_AssociativeOriginData5.AssociatedPoint = NULL;
annotation_AssociativeOriginData5.OffsetAnnotation = note1;
annotation_AssociativeOriginData5.OffsetAlignmentPosition = Annotations::AlignmentPositionMidCenter;
annotation_AssociativeOriginData5.XOffsetFactor = 40;
annotation_AssociativeOriginData5.YOffsetFactor = 8;
NXOpen::Point3d point3d9(33.1533828382838, 3.8529702970297, 0);
note5->SetAssociativeOrigin(annotation_AssociativeOriginData5,point3d9);
theSession->UpdateManager()->DoUpdate(markId1);
markId1 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Create Annotation");
// ----------------------------------------------------------
//   This adds in the note for the Author name
//   
//  We set up an association, to the title block note, so 
//  when the title block is moved the text will travel with it
//------------------------------------------------------------
stringArray1[0]=  "<C3.250>" + authorName1 + "<C>";
Annotations::Note *note6 = theSession->Parts()->Work()->Annotations()->CreateNote(stringArray1,point3d2,AxisOrientationHorizontal,letteringPreferences1,userSymbolPreferences1);
// ----------------------------------------------
//   Menu: Edit->Placement->Origin Tool
// ----------------------------------------------
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
Annotations::Annotation::AssociativeOriginData annotation_AssociativeOriginData6;
annotation_AssociativeOriginData6.OriginType = Annotations::AssociativeOriginTypeOffsetFromText;
annotation_AssociativeOriginData6.View = NULL;
annotation_AssociativeOriginData6.ViewOfGeometry = NULL;
annotation_AssociativeOriginData6.PointOnGeometry = NULL;
annotation_AssociativeOriginData6.VertAnnotation = NULL;
annotation_AssociativeOriginData6.VertAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData6.HorizAnnotation = NULL;
annotation_AssociativeOriginData6.HorizAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData6.AlignedAnnotation = NULL;
annotation_AssociativeOriginData6.DimensionLine = 0;
annotation_AssociativeOriginData6.AssociatedView = NULL;
annotation_AssociativeOriginData6.AssociatedPoint = NULL;
annotation_AssociativeOriginData6.OffsetAnnotation = note1;
annotation_AssociativeOriginData6.OffsetAlignmentPosition = Annotations::AlignmentPositionMidCenter;
annotation_AssociativeOriginData6.XOffsetFactor = 40;
annotation_AssociativeOriginData6.YOffsetFactor = 1;
NXOpen::Point3d point3d11(33.1533828382838, 2.9779702970297, 0);
note6->SetAssociativeOrigin(annotation_AssociativeOriginData6,point3d11);
theSession->UpdateManager()->DoUpdate(markId1);
markId1 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Create Annotation");
// ----------------------------------------------------------
//   This adds in the note for the Date 
//   
//  We set up an association, to the title block note, so 
//  when the title block is moved the text will travel with it
//------------------------------------------------------------
stringArray1[0]=  "<C3.250>" + currDate + "<C>";
Annotations::Note *note7 = theSession->Parts()->Work()->Annotations()->CreateNote(stringArray1,point3d2,AxisOrientationHorizontal,letteringPreferences1,userSymbolPreferences1);
// ----------------------------------------------
//   Menu: Edit->Placement->Origin Tool
// ----------------------------------------------
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
Annotations::Annotation::AssociativeOriginData annotation_AssociativeOriginData7;
annotation_AssociativeOriginData7.OriginType = Annotations::AssociativeOriginTypeOffsetFromText;
annotation_AssociativeOriginData7.View = NULL;
annotation_AssociativeOriginData7.ViewOfGeometry = NULL;
annotation_AssociativeOriginData7.PointOnGeometry = NULL;
annotation_AssociativeOriginData7.VertAnnotation = NULL;
annotation_AssociativeOriginData7.VertAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData7.HorizAnnotation = NULL;
annotation_AssociativeOriginData7.HorizAlignmentPosition = Annotations::AlignmentPositionTopLeft;
annotation_AssociativeOriginData7.AlignedAnnotation = NULL;
annotation_AssociativeOriginData7.DimensionLine = 0;
annotation_AssociativeOriginData7.AssociatedView = NULL;
annotation_AssociativeOriginData7.AssociatedPoint = NULL;
annotation_AssociativeOriginData7.OffsetAnnotation = note1;
annotation_AssociativeOriginData7.OffsetAlignmentPosition = Annotations::AlignmentPositionMidCenter;
annotation_AssociativeOriginData7.XOffsetFactor = 40;
annotation_AssociativeOriginData7.YOffsetFactor = -7;
NXOpen::Point3d point3d13(33.1533828382838, 2.9779702970297, 0);
note7->SetAssociativeOrigin(annotation_AssociativeOriginData7,point3d13);
theSession->UpdateManager()->DoUpdate(markId1);
markId1 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Create Annotation");
theSession->SetUndoMarkVisibility(markId1, "Create Annotation", Session::MarkVisibilityVisible);
delete letteringPreferences1;
delete userSymbolPreferences1;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarcoPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值