003.UG 2512全新版本 二次开发高级草图类封装

003.UG 2512全新版本 二次开发高级草图类封装
从UG12.0版本开始,西门子在自废武功的道路上越行越远, 主动放弃了ug软件机械设计模块功能,草图求解器也从约束改为了松弛尺寸,推断约束等,与专业严谨背道而驰, 在傲慢之罪的道路上固执地行走了几十年,大量中国用户被这道墙隔离了几十年,职场流行的依然是几十年前的旧版为主,因为12.0版本以后,西门子就刻意切断了中国用户使用便利性,让新版本工作效率降低几倍以上,甚至不能用于实际设计工作.整个软件发展改向了加工仿真,模具等模块,虽然在这种情况下专研新版西门子软件是非常不理智行为,但由于几十年的技术段差,ug的openg api也隔了几代迭代更新,对于高阶二次开发用户,使用新版api能简单实现定制化功能,代码逻辑性更强,开发效益高(仅指编程开发人员),使用新版本api势在必行.因为,我们不可能一直用几十年前的技术去研究西门子一.本系列使用的编程api使用最新版本API 西门子1872版本以后陆续更新的openg api,也包含一些西门子未更新迭代的旧的旧api技术,全面拥抱新西门子api技术,在未来数年,十年,本系列api代码兼容性依然主流(西门子并无强大自主开发能力,核心技术主要买自美国)注意:本系列开发的插件仅保证ug2512以后的新版本ug能正常运行使用,旧版本兼容性未做过测试二.基础草图类的封装此次草图类的封装只提供一个创建ug草图基本功能的壳,为保持草图类干净简洁,不封装任何绘图,约束功能等,只提供一个草图壳,后续需要画图的功能封装到别的算法类,具有基础功能的草图类:using NXOpen; using System; namespace UG_API.NX.Sketch { /// summary /// ZC平面任务草图 /// /summary public class ZC_Sketch { NXOpen.Session theSession; NXOpen.Part workPart; NXOpen.Part displayPart; public Draw cad { get; } //草图算法类,绘制草图 public ZC_Sketch() { // 获取当前NX会话和工作部件 theSession NXOpen.Session.GetSession(); workPart theSession.Parts.Work; displayPart theSession.Parts.Display; cad new Draw(theSession, workPart, displayPart);//构建草图算法类 // 开始任务环境 theSession.BeginTaskEnvironment(); // 创建简单草图构建器 NXOpen.SimpleSketchInPlaceBuilder simpleSketchInPlaceBuilder1; simpleSketchInPlaceBuilder1 workPart.Sketches.CreateSimpleSketchInPlaceBuilder(); simpleSketchInPlaceBuilder1.UseWorkPartOrigin false; // 创建坐标系所需的几何元素 NXOpen.Point3d coordinates2 new NXOpen.Point3d(0.0, 0.0, 0.0); NXOpen.Point point2 workPart.Points.CreatePoint(coordinates2); // 创建方向 - 修正为XC方向 NXOpen.Point3d origin4 new NXOpen.Point3d(0.0, 0.0, 0.0); NXOpen.Vector3d vector2 new NXOpen.Vector3d(1.0, 0.0, 0.0); // 修正为XC方向 NXOpen.Direction direction2 workPart.Directions.CreateDirection( origin4, vector2, NXOpen.SmartObject.UpdateOption.WithinModeling); // 创建ZC平面 NXOpen.Point3d origin5 new NXOpen.Point3d(0.0, 0.0, 0.0); NXOpen.Matrix3x3 matrix2 new NXOpen.Matrix3x3(); // 修正矩阵Z轴为(0,0,1)表示ZC平面 matrix2.Xx 1.0; matrix2.Xy 0.0; matrix2.Xz 0.0; // X轴 matrix2.Yx 0.0; matrix2.Yy 1.0; matrix2.Yz 0.0; // Y轴 matrix2.Zx 0.0; matrix2.Zy 0.0; matrix2.Zz 1.0; // Z轴 (法向) NXOpen.Plane plane3 workPart.Planes.CreateFixedTypePlane( origin5, matrix2, NXOpen.SmartObject.UpdateOption.WithinModeling); // 创建变换和坐标系 NXOpen.Xform xform1 workPart.Xforms.CreateXformByPlaneXDirPoint( plane3, direction2, point2, NXOpen.SmartObject.UpdateOption.WithinModeling, 0.625, false, false); NXOpen.CartesianCoordinateSystem cartesianCoordinateSystem1 workPart.CoordinateSystems.CreateCoordinateSystem(xform1, NXOpen.SmartObject.UpdateOption.WithinModeling); simpleSketchInPlaceBuilder1.CoordinateSystem cartesianCoordinateSystem1; // 设置水平参考 NXOpen.DatumAxis datumAxis1 ((NXOpen.DatumAxis)workPart.Datums.FindObject(DATUM_CSYS(0) X axis)); simpleSketchInPlaceBuilder1.HorizontalReference.Value datumAxis1; NXOpen.Point point3 simpleSketchInPlaceBuilder1.SketchOrigin; simpleSketchInPlaceBuilder1.SketchOrigin point3; // 设置草图参数 theSession.Preferences.Sketch.CreateInferredConstraints false; theSession.Preferences.Sketch.ContinuousAutoDimensioning false; theSession.Preferences.Sketch.DimensionLabel NXOpen.Preferences.SketchPreferences.DimensionLabelType.Value; theSession.Preferences.Sketch.TextSizeFixed false; theSession.Preferences.Sketch.FixedTextSize 3.0; theSession.Preferences.Sketch.DisplayParenthesesOnReferenceDimensions true; theSession.Preferences.Sketch.DisplayReferenceGeometry false; theSession.Preferences.Sketch.DisplayShadedRegions true; theSession.Preferences.Sketch.FindMovableObjects true; theSession.Preferences.Sketch.ConstraintSymbolSize 3.0; theSession.Preferences.Sketch.DisplayObjectColor false; theSession.Preferences.Sketch.DisplayObjectName true; theSession.Preferences.Sketch.EditDimensionOnCreation false; theSession.Preferences.Sketch.CreateDimensionForTypedValues true; // 创建草图 NXOpen.NXObject nXObject1 simpleSketchInPlaceBuilder1.Commit(); NXOpen.Sketch sketch1 ((NXOpen.Sketch)nXObject1); NXOpen.Features.Feature feature1 sketch1.Feature; // 更新并激活草图 theSession.UpdateManager.DoUpdate(theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, update)); sketch1.Activate(NXOpen.Sketch.ViewReorient.True); // 查找可移动对象 NXOpen.SketchFindMovableObjectsBuilder sketchFindMovableObjectsBuilder1 workPart.Sketches.CreateFindMovableObjectsBuilder(); NXOpen.NXObject nXObject2 sketchFindMovableObjectsBuilder1.Commit(); sketchFindMovableObjectsBuilder1.Destroy(); // 清理构建器 simpleSketchInPlaceBuilder1.Destroy(); // 设置草图名称 // theSession.ActiveSketch.SetName(SKETCH_000);//对于非新建的ug零件,改名会冲突 theSession.CleanUpFacetedFacesAndEdges(); } /// summary /// 检查草图是否完全约束,约束之前只有调用这个,才能刷新草图判断草图是否完全约束 /// /summary public void examine() { // 再次查找可移动对象 NXOpen.SketchFindMovableObjectsBuilder sketchFindMovableObjectsBuilder2 workPart.Sketches.CreateFindMovableObjectsBuilder(); NXOpen.NXObject nXObject3 sketchFindMovableObjectsBuilder2.Commit(); sketchFindMovableObjectsBuilder2.Destroy(); theSession.CleanUpFacetedFacesAndEdges(); } /// summary /// 完成并退出草图,必须调用 /// /summary public void end() { // 完成草图 NXOpen.SketchWorkRegionBuilder sketchWorkRegionBuilder1 workPart.Sketches.CreateWorkRegionBuilder(); sketchWorkRegionBuilder1.Scope NXOpen.SketchWorkRegionBuilder.ScopeType.EntireSketch; NXOpen.NXObject nXObject4 sketchWorkRegionBuilder1.Commit(); sketchWorkRegionBuilder1.Destroy(); theSession.ActiveSketch.CalculateStatus(); theSession.Preferences.Sketch.SectionView false; theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.True, NXOpen.Sketch.UpdateLevel.Model); theSession.DeleteUndoMarksSetInTaskEnvironment(); theSession.EndTaskEnvironment(); } } }二.绘图功能封装在其他算法类using NXOpen; using NXOpen.CAE; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UG_API.NX.Sketch { /// summary /// 几何算法 /// /summary public class Draw { NXOpen.Session theSession; NXOpen.Part workPart; NXOpen.Part displayPart; NXOpen.NXMatrix nXMatrix1; public Draw(NXOpen.Session _theSession, NXOpen.Part _workPart, NXOpen.Part _displayPart) { theSession _theSession; workPart _workPart; displayPart _displayPart; } /// summary /// 绘制直线算法,根据空间点坐标 /// /summary public void Line(NXOpen.Point3d start, NXOpen.Point3d end) { NXOpen.Line line1 workPart.Curves.CreateLine(start, end); theSession.ActiveSketch.AddGeometry(line1, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints); theSession.ActiveSketch.Update(); } /// summary /// 绘制直线算法,根据两点二维坐标 /// /summary /// param namex0/param /// param namey0/param /// param namex1/param /// param namey1/param public void Line(double x0,double y0,double x1 ,double y1) { // 绘制直线 - 修正Z坐标为0 NXOpen.Point3d startPoint1 new NXOpen.Point3d(x0, y0, 0.0); NXOpen.Point3d endPoint1 new NXOpen.Point3d(x1, y1, 0.0); NXOpen.Line line1 workPart.Curves.CreateLine(startPoint1, endPoint1); theSession.ActiveSketch.AddGeometry(line1, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints); theSession.ActiveSketch.Update(); } /// summary /// 绘制直线(极坐标) /// /summary /// param namex0起点x坐标/param /// param namey0起点y坐标/param /// param namelength线长度/param /// param nameangleDegrees与x轴的夹角/param public void LinePolar(double x0, double y0, double length, double angleDegrees) { // 将角度转换为弧度[1](ref) double angleRadians angleDegrees * Math.PI / 180.0; // 计算终点坐标x x0 length * cos(θ), y y0 length * sin(θ)[1](ref) double x1 x0 length * Math.Cos(angleRadians); double y1 y0 length * Math.Sin(angleRadians); // 调用现有的直线绘制方法 Line(x0, y0, x1, y1); } /// summary /// 根据3点绘制圆弧 /// /summary /// param namecenter中心点坐标/param /// param nameR半径/param /// param nameStarAngl 开始角度/param /// param nameEndAngl结束角度/param public void Arc(NXOpen.Point3d center, double R, double StarAngl, double EndAngl) { nXMatrix1 theSession.ActiveSketch.Orientation;//设置将圆弧画在草图平面内(设置为非空间圆弧) NXOpen.Part workPart theSession.Parts.Work; NXOpen.Arc arc1;//构建一个圆弧对象 //workPart.Curves.CreateArc()这是 NX Open 中创建圆弧的方法,******center1: 圆心。***nXMatrix1: 圆弧所在的平面矩阵,,R: 圆弧的半径。 arc1 workPart.Curves.CreateArc(center, nXMatrix1, R, (StarAngl * Math.PI / 180.0), (EndAngl * Math.PI / 180.0)); theSession.ActiveSketch.AddGeometry(arc1, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints);//将圆弧添加到草图中,设置不要为圆弧添加约束InferNoConstraints,防止圆弧变形出错 } /// summary /// 绘制圆,并固定圆心位置,加约束直径 (完全约束) /// /summary /// param namex圆心x坐标/param /// param namey圆心y坐标/param /// param nameR约束半径/param public void Arc(double x,double y,double R) { // 2. 创建圆 nXMatrix1 theSession.ActiveSketch.Orientation; NXOpen.Point3d center1 new NXOpen.Point3d(x, y, 0.0); NXOpen.Arc arc1 workPart.Curves.CreateArc(center1, nXMatrix1, R, 0.0, 2.0 * Math.PI); theSession.ActiveSketch.AddGeometry(arc1, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints); theSession.ActiveSketch.Update(); // 3. 固定圆心约束 NXOpen.SketchFixObjectsBuilder fixBuilder workPart.Sketches.CreateSketchFixObjectsBuilder(); NXOpen.Sketch.ConstraintGeometry fixGeo new NXOpen.Sketch.ConstraintGeometry(); fixGeo.Geometry arc1; fixGeo.PointType NXOpen.Sketch.ConstraintPointType.ArcCenter; fixGeo.SplineDefiningPointIndex 0; fixBuilder.AddObject(fixGeo); fixBuilder.Commit(); fixBuilder.Destroy(); // 4.尺寸约束构造器 NXOpen.SketchRapidDimensionBuilder dimBuilder workPart.Sketches.CreateRapidDimensionBuilder(); // 设置为直径测量 dimBuilder.Measurement.Method NXOpen.Annotations.DimensionMeasurementBuilder.MeasurementMethod.Diametral; dimBuilder.FirstAssociativity.SetValue(arc1, null, center1); //将约束直径尺寸关联到 arc1 // 设置尺寸值(直径) dimBuilder.Driving.DimensionValue R*2; // 提交 NXOpen.Session.UndoMarkId markId theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, CreateSketchDimension);//记号点,高级编程用,可删 dimBuilder.Commit();//提交尺寸约束 dimBuilder.Destroy();//销毁尺寸约束构造器 // 5. 保持环境状态 theSession.ActiveSketch.CalculateStatus();//强制刷新草图求解器 theSession.ActiveSketch.Update();//立即更新草图 } } }