008.UG二次开发,拉伸建模类封装及高级草图类优化,对象传参

008.UG二次开发,拉伸建模类封装及高级草图类优化,对象传参
前面的章节我们掌握了如何新建一个草图,绘制各种各样的几何草图等等,但他在ug软件中草图是一个独立的特征对象,软件内部自动机制会自动给草图起名编号等等,传统的建模是通过这些硬编码草图名去寻找草图名完成拉伸,旋转等建模工作.这种硬编码的方式有一非常致命缺点,每一个建模命令代码都是唯一的,一旦重复使用,内部机制就会混乱出错,导致软件崩溃.作为二次开发人员,我们要打造的api类就是要能被重复调用,即我们只需要写一套例如拉伸功能的代码,后面所有拉伸建模的步骤都可以调用同一个api,通过传参不同的草图对象,去拉伸生成不同的实体,通过这种以草图对象精准控制拉伸建模的方式,改变ug软件这种致命的旧运行机制,以全新合理的逻辑控制建模一.草图类优化,增加通用对象传参属性在草图类增加一个nx通用对象属性public NXOpen.NXObject nXObject1 { get; set; }//草图对象存储属性通过草图构造器.commit获得单前草图的nXobject对象,存进属性nXObject1 simpleSketchInPlaceBuilder1.Commit();完整的草图类如下using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 NXOpen.NXObject nXObject1 { get; set; }//草图对象存储属性 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; // 创建草图 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(); } } }二.拉伸建模类的封装如图,ug的拉伸功能会根据你选择的草图,将草图拉伸为指定高度的实体,拉伸功能还有许多高级功能:如布尔,拔模,偏置,设置实体或片体等,这里我们只演示拉伸基本功能1.基础拉伸类封装完整的拉伸类如下:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UG_API.NX.Modeling { /// summary /// 拉伸草图生成实体类 /// nXObject传入的草图对象, star拉伸起点高度,H拉伸终点的高度 /// /summary public class UG_X { public void model(NXOpen.NXObject nXObject, double star, double H) { // 获取当前 NX 会话、工作部件和显示部件 NXOpen.Session theSession NXOpen.Session.GetSession(); NXOpen.Part workPart theSession.Parts.Work; NXOpen.Part displayPart theSession.Parts.Display; // 创建拉伸构建器ExtrudeBuilder参数为 null 表示创建新特征 NXOpen.Features.Feature nullNXOpen_Features_Feature null; NXOpen.Features.ExtrudeBuilder extrudeBuilder1; extrudeBuilder1 workPart.Features.CreateExtrudeBuilder(nullNXOpen_Features_Feature); // 创建截面Section用于定义拉伸的轮廓 NXOpen.Section section1; section1 workPart.Sections.CreateSection(0.00095, 0.001, 0.050000000000000003); // 将截面分配给拉伸构建器 extrudeBuilder1.Section section1; // 允许自相交截面 extrudeBuilder1.AllowSelfIntersectingSection(true); // 获取单位信息用于后续表达式创建 NXOpen.Unit unit1 extrudeBuilder1.Draft.FrontDraftAngle.Units; // 创建一个系统表达式用于设置拔模角度等参数 NXOpen.Expression expression1 workPart.Expressions.CreateSystemExpressionWithUnits(2.00, unit1); // 设置距离公差 extrudeBuilder1.DistanceTolerance 0.001; // 设置布尔运算类型为创建新建实体 extrudeBuilder1.BooleanOperation.Type NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create; // 初始化目标体数组这里设为 null表示不与现有实体求交/并/减 NXOpen.Body[] targetBodies1 new NXOpen.Body[1]; NXOpen.Body nullNXOpen_Body null; targetBodies1[0] nullNXOpen_Body; extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies1); // 设置拉伸起始距离为 0 extrudeBuilder1.Limits.StartExtend.Value.SetFormula(star.ToString());//拉伸起点高度 // 设置拉伸结束距离为 20 extrudeBuilder1.Limits.EndExtend.Value.SetFormula(H.ToString());//拉伸终点高度 // 设置前后拔模角度为 2 度 extrudeBuilder1.Draft.FrontDraftAngle.SetFormula(2); extrudeBuilder1.Draft.BackDraftAngle.SetFormula(2); // 设置起始和结束偏移量 extrudeBuilder1.Offset.StartOffset.SetFormula(0); extrudeBuilder1.Offset.EndOffset.SetFormula(5); // 配置智能体积轮廓构建器用于处理复杂截面 NXOpen.GeometricUtilities.SmartVolumeProfileBuilder smartVolumeProfileBuilder1 extrudeBuilder1.SmartVolumeProfile; smartVolumeProfileBuilder1.OpenProfileSmartVolumeOption false; smartVolumeProfileBuilder1.CloseProfileRule NXOpen.GeometricUtilities.SmartVolumeProfileBuilder.CloseProfileRuleType.Fci; // 设置截面参数 section1.DistanceTolerance 0.001; section1.ChainingTolerance 0.00095; section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves); // 创建选择意图规则选项 NXOpen.SelectionIntentRuleOptions selectionIntentRuleOptions1 workPart.ScRuleFactory.CreateRuleOptions(); selectionIntentRuleOptions1.SetSelectedFromInactive(false); // 查找名为 SKETCH(9) 的草图特征 NXOpen.Features.Feature[] features1 new NXOpen.Features.Feature[1]; NXOpen.Features.SketchFeature sketchFeature1 ((NXOpen.Features.SketchFeature)workPart.Features.GetAssociatedFeature(nXObject));//查找草图 features1[0] sketchFeature1; // 创建曲线特征规则基于草图特征 NXOpen.DisplayableObject nullNXOpen_DisplayableObject null; NXOpen.CurveFeatureRule curveFeatureRule1 workPart.ScRuleFactory.CreateRuleCurveFeature(features1, nullNXOpen_DisplayableObject, selectionIntentRuleOptions1); selectionIntentRuleOptions1.Dispose(); // 释放规则选项资源 section1.AllowSelfIntersection(true); section1.AllowDegenerateCurves(false); // 将草图曲线添加到截面中 NXOpen.SelectionIntentRule[] rules1 new NXOpen.SelectionIntentRule[1]; rules1[0] curveFeatureRule1; NXOpen.NXObject nullNXOpen_NXObject null; NXOpen.Point3d helpPoint1 new NXOpen.Point3d(0.0, 0.0, 0.0); section1.AddToSection(rules1, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, false); // 获取草图对象并创建拉伸方向 NXOpen.Sketch sketch1 ((NXOpen.Sketch)sketchFeature1.Sketch); NXOpen.Direction direction1 workPart.Directions.CreateDirection(sketch1, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling); extrudeBuilder1.Direction direction1; // 最终确认布尔运算类型 extrudeBuilder1.BooleanOperation.Type NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create; extrudeBuilder1.BooleanOperation.SetTargetBodies(new NXOpen.Body[0]); // 清空目标体以确保新建实体 // 提交特征在模型中生成拉伸体 NXOpen.Features.Feature feature1 extrudeBuilder1.CommitFeature(); // 清理工作销毁构建器删除临时创建的表达式 NXOpen.Expression expression2 extrudeBuilder1.Limits.StartExtend.Value; NXOpen.Expression expression3 extrudeBuilder1.Limits.EndExtend.Value; extrudeBuilder1.Destroy(); workPart.Expressions.Delete(expression1); } } }2.草图对象传参前面说到,ug拉伸功能是根据用户选择的草图,将草图拉伸成实体,软件后台怎么拿到这个草图,前面说的传统内部机制是其原理,我们不细讲,因为我们不打算用这种落后的机制.我们将自己编写全新合理的运行逻辑来获取草图.草图对象传参,拉伸类model方法接收一个草图对象nXobject,这个对象就是刚刚我们在草图类对象新建的通用对象属性,他存储了草图对象,接收了他,我们的拉伸类对象就有了一个明确指向的草图public void model(NXOpen.NXObjectnXObject,doublestar,doubleH){}doublestar,doubleH是后续我们用来控制拉伸高度的起点和终点位置3.使用草图对象nXObject原理我不细讲了,如下,已经封装好在类里了,这段代码会将nXObject对象转换拉伸草图特征,后续被拉伸为实体// 查找名为 SKETCH(9) 的草图特征 NXOpen.Features.Feature[] features1 new NXOpen.Features.Feature[1]; NXOpen.Features.SketchFeature sketchFeature1 ((NXOpen.Features.SketchFeature)workPart.Features.GetAssociatedFeature(nXObject));//查找草图 features1[0] sketchFeature1;三.使用方法传入了Dwg.nXObject1草图对象,拉伸实体using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UG_API.NX.Modeling; using UG_API.NX.Sketch; namespace UG_API.NX.Screw { /// summary /// 生成螺目模型 /// /summary public class Screw_modeling { public void rest(double D1, double D, double H) { ZC_Sketch Dwg new ZC_Sketch(); Dwg.cad.Arc(0,0,D1/2); double num test(D); Dwg.cad.Hexagon(num);//绘制六边形草图 Dwg.end();//结束草图 UG_X x new UG_X();//拉伸类 x.model(Dwg.nXObject1,0,H);//拉伸草图生产实体 } public double test(double D) { if (D 8) { return 7.5; } else if (D 10) { return 9.23; } else if (D 12) { return 10.39; } else if (D 16) { return 13.85; } else if (D 20) { return 17.32; } else if (D 24) { return 20.78; } else { return 23.67; } } } }运行结果如下:他是由我们自己设计的插件自动建模生产的实体,也就说,他是后台完全自动化建模完成,这就是我们二次开发的意义

最新新闻

日新闻

周新闻

月新闻