您的位置:网站首页 > UG教程下载

UG二次开发学习进阶帮手——操作录制

时间:2010-11-15 10:32:37 来源:

  什么是操作录制?这是UG NX系列中增加的可以录制手工操作命令的工具。录制的一系列NXOpen程序命令。通过这些命令可以重现操作。当然,从中我们也可以用来学习NXOpen。

  然而,录制的程序往往是记录我们手工操作的命令流,其中很多信息是固定的操作,不具有灵活性。可以重现整个手工操作过程。同时,不是所有的命令都能够被录制。正如UG Help系统所说的:有些命令不能被记录,有些命令只能部分记录。正如我以前的帖子说过,UG有个两个开发包Open和NXOpen,Open主要是API和包装 API的类。NXOpen是类库。操作录制的结果是NXOpen程序形式。但是Open在某些细节上要强于NXOpen。所以,操作录制不能代替二次开发。

  虽然如此,UG操作录制确实有很多意义:通过文本重现整个操作;改变程序内容可以适度的变动操作过程,不用重做整个操作;通过这个记录可以帮助我们学习NXOpen程序。

  现在,我们来录制一个创建block的命令流。

  我用的是NX6,其他版本差不多。

  在菜单【首选项】里选择【用户界面】。

  在对话框“用户界面首选项”中的【操作记录】Tab中选择【选择操作记录语言】。

  UG NX6支持java、C#、C++等语言命令录制,其中C#是NX6新加入支持的。

  现在在菜单【工具】中选择【操作记录】,其中有下面三个常用命令:

  录制-暂停录制-停止录制

  当然也可以选择工具条【操作录制】。

  选择【录制】,选择保存录制命令程序的目录。

  之后UG的菜单和工具条上会出现可录制的标示,如果没有的命令是不能被录制的。

  

  选择block命令弹出创建block的对话框。

  我就是改了长宽高为10、20、30,点击确定生成block。

  点击保存按钮。

  选择【停止录制】结束本次录制。

  下面我们将说说这个录制的程序。

  view plaincopy to clipboardprint?

  ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  // NX 6.0.0.24

  // Journal created by nicholas on Tue Feb 17 21:18:57 2009 中国标准时间

  //

  using System;

  using NXOpen;

  public class NXJournal

  {

  public static void Main(string[] args)

  {

  Session theSession = Session.GetSession();

  Part workPart = theSession.Parts.Work;

  Part displayPart = theSession.Parts.Display;

  // ----------------------------------------------

  // 菜单:插入->Design Feature->Block...

  // ----------------------------------------------

  NXOpen.Session.UndoMarkId markId1;

  markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start");

  NXOpen.Features.Feature nullFeatures_Feature = null;

  NXOpen.Features.BlockFeatureBuilder blockFeatureBuilder1;

  blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature);

  blockFeatureBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;

  Body[] targetBodies1 = new Body[1];

  Body nullBody = null;

  targetBodies1[0] = nullBody;

  blockFeatureBuilder1.BooleanOption.SetTargetBodies(targetBodies1);

  theSession.SetUndoMarkName(markId1, "Block 对话框");

  NXOpen.Session.UndoMarkId markId2;

  markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Block");

  blockFeatureBuilder1.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths;

  Point point1;

  point1 = blockFeatureBuilder1.OriginPoint;

  blockFeatureBuilder1.OriginPoint = point1;

  Point3d originPoint1 = new Point3d(0.0, 0.0, 0.0);

  blockFeatureBuilder1.SetOriginAndLengths(originPoint1, "10", "20", "30");

  blockFeatureBuilder1.SetBooleanOperationAndTarget(NXOpen.Features.Feature.BooleanType.Create, nullBody);

  NXOpen.Features.Feature feature1;

  feature1 = blockFeatureBuilder1.CommitFeature();

  theSession.DeleteUndoMark(markId2, null);

  theSession.SetUndoMarkName(markId1, "Block");

  blockFeatureBuilder1.Destroy();

  workPart.FacetedBodies.DeleteTemporaryFacesAndEdges();

  // ----------------------------------------------

  // 菜单:文件->保存

  // ----------------------------------------------

  PartSaveStatus partSaveStatus1;

  partSaveStatus1 = workPart.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False);

  partSaveStatus1.Dispose();

  // ----------------------------------------------

  // 菜单:工具->操作记录->Stop Recording

  // ----------------------------------------------

  }

  public static int GetUnloadOption(string dummy) { return (int)Session.LibraryUnloadOption.Immediately; }

  }

  // NX 6.0.0.24

  // Journal created by nicholas on Tue Feb 17 21:18:57 2009 中国标准时间

  //

  using System;

  using NXOpen;

  public class NXJournal

  {

  public static void Main(string[] args)

  {

  Session theSession = Session.GetSession();

  Part workPart = theSession.Parts.Work;

  Part displayPart = theSession.Parts.Display;

  // ----------------------------------------------

  // 菜单:插入->Design Feature->Block...

  // ----------------------------------------------

  NXOpen.Session.UndoMarkId markId1;

  markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start");

  NXOpen.Features.Feature nullFeatures_Feature = null;

  NXOpen.Features.BlockFeatureBuilder blockFeatureBuilder1;

  blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature);

  blockFeatureBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;

  Body[] targetBodies1 = new Body[1];

  Body nullBody = null;

  targetBodies1[0] = nullBody;

  blockFeatureBuilder1.BooleanOption.SetTargetBodies(targetBodies1);

  theSession.SetUndoMarkName(markId1, "Block 对话框");

  NXOpen.Session.UndoMarkId markId2;

  markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Block");

  blockFeatureBuilder1.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths;

  Point point1;

  point1 = blockFeatureBuilder1.OriginPoint;

  blockFeatureBuilder1.OriginPoint = point1;

  Point3d originPoint1 = new Point3d(0.0, 0.0, 0.0);

  blockFeatureBuilder1.SetOriginAndLengths(originPoint1, "10", "20", "30");

  blockFeatureBuilder1.SetBooleanOperationAndTarget(NXOpen.Features.Feature.BooleanType.Create, nullBody);

  NXOpen.Features.Feature feature1;

  feature1 = blockFeatureBuilder1.CommitFeature();

  theSession.DeleteUndoMark(markId2, null);

  theSession.SetUndoMarkName(markId1, "Block");

  blockFeatureBuilder1.Destroy();

  workPart.FacetedBodies.DeleteTemporaryFacesAndEdges();

  // ----------------------------------------------

  // 菜单:文件->保存

  // ----------------------------------------------

  PartSaveStatus partSaveStatus1;

  partSaveStatus1 = workPart.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False);

  partSaveStatus1.Dispose();

  // ----------------------------------------------

  // 菜单:工具->操作记录->Stop Recording

  // ----------------------------------------------

  }

  public static int GetUnloadOption(string dummy) { return (int)Session.LibraryUnloadOption.Immediately; }

  }

  Main函数是入口函数。

  11-13行是常用变量的声明。

  17-57是建立block的命令记录。

  其中,17-18是声明UndoMark。相当于事务,可以用来回滚程序影响。

  UG默认每个命令都设置了UndoMark,所以如果用用UndoMark块的程序效率较低,采用UndoMark声明中间的程序不在做程序你的UndoMark。对效率提高有帮助。

  51-53结束UndoMark块。

  22NXOpen采用类厂模式,workPart.Features.CreateBlockFeatureBuilder是产生一个对象。

  49 feature1 = blockFeatureBuilder1.CommitFeature();生成特征

  62-65点击保存按钮的命令录制。