UG创建最大边界盒

 1 using System;
 2 using NXOpen;
 3 using NXOpen.UF;
 4 using System.Windows.Forms;
 5 
 6 public class NXJournal
 7 {
 8   public static void Main(string[] args)
 9   {
10        NXOpen.Session theSession = NXOpen.Session.GetSession();
11     NXOpen.Part workPart = theSession.Parts.Work;
12     NXOpen.Part displayPart = theSession.Parts.Display;
13     UI theUI = UI.GetUI();
14     UFSession theUfSession = UFSession.GetUFSession();
15 
16     
17             //第一种方法
18 
19             TaggedObject tag;
20             Point3d point3d;
21 
22             ////选择对象
23             theUI.SelectionManager.SelectTaggedObject("选择对象", "创建边界盒", Selection.SelectionScope.WorkPartAndOccurrence, false, true, out tag, out point3d);
24 
25             ////获取对象的最大对角点
26             ////是以绝对坐标获取的最大外围,如果坐标摆斜了创建的方块就不对.
27             //double[] outlinePoint = new double[6];
28             //NXOpen.UF.UFSession.GetUFSession().Modl.AskBoundingBox(tag.Tag, outlinePoint);
29 
30             ////创建一个点
31             //Point point = theSession.Parts.Work.Points.CreatePoint(new Point3d(outlinePoint[0], outlinePoint[1], outlinePoint[2]));
32             ////Point point_1 = theSession.Parts.Work.Points.CreatePoint(new Point3d(box[3], box[4], box[5]));
33 
34             ////创建点特征(真正的创建)
35             //NXOpen.Features.Feature pointFeature = null;
36             //NXOpen.Features.PointFeatureBuilder pointFeatureBuilder = theSession.Parts.Work.BaseFeatures.CreatePointFeatureBuilder(pointFeature);
37             //pointFeatureBuilder.Point = point;
38             //pointFeatureBuilder.Commit();
39             //pointFeatureBuilder.Destroy();
40 
41             //////创建最大外围盒
42             ////NXOpen.Features.Block block = null;
43             ////NXOpen.Features.BlockFeatureBuilder blockFeatureBuilder;
44             ////blockFeatureBuilder = theSession.Parts.Work.Features.CreateBlockFeatureBuilder(block);
45 
46             ////blockFeatureBuilder.SetTwoDiagonalPoints
47             ////    (new Point3d(outlinePoint[0], outlinePoint[1], outlinePoint[2]), new Point3d(outlinePoint[3], outlinePoint[4], outlinePoint[5]));
48 
49             ////block = (NXOpen.Features.Block)blockFeatureBuilder.Commit();
50             ////blockFeatureBuilder.Destroy();
51 
52             //////设置块的名称
53             ////block.SetName("rrr");
54             //////取消或设置高亮
55             ////NXOpen.UF.UFSession.GetUFSession().Disp.SetHighlight(tag.Tag, 0);
56 
57 
58             //第二种方法
59             //按wcs坐标系
60             Tag UFWcs;//
61             NXOpen.UF.UFSession.GetUFSession().Csys.AskWcs(out UFWcs);
62             Tag wcs = theSession.Parts.Work.WCS.CoordinateSystem.Tag;
63 
64             bool expand = true;
65             double[] min_corner = new double[3]; //角落点
66             double[,] directions = new double[3,3];//方向
67             double[] distances = new double[3];//xyz最大距离
68             NXOpen.UF.UFSession.GetUFSession().Modl.AskBoundingBoxAligned
69                 (tag.Tag, wcs, expand, min_corner, directions, distances);
70 
71 
72             //创建最大外围盒
73             NXOpen.Features.Block block = null;
74             NXOpen.Features.BlockFeatureBuilder blockFeatureBuilder;
75             blockFeatureBuilder = theSession.Parts.Work.Features.CreateBlockFeatureBuilder(block);
76 
77             blockFeatureBuilder.SetOriginAndLengths
78                 (new Point3d(min_corner[0], min_corner[1], min_corner[2]),
79                 distances[0].ToString(), distances[1].ToString(), distances[2].ToString());
80 
81             block = (NXOpen.Features.Block)blockFeatureBuilder.Commit();
82             blockFeatureBuilder.Destroy();
83 
84 
85             //取消或设置高亮
86             NXOpen.UF.UFSession.GetUFSession().Disp.SetHighlight(tag.Tag, 0);
87 
88 
89             //第3种方法
90 
91            //  NXOpen.UF.UFSession.GetUFSession().Modl.AskBoundingBoxExact(tag.Tag,theSession.Parts.Work.WCS.Tag,)
92     
93   }
94   public static int GetUnloadOption(string dummy) { return (int)NXOpen.Session.LibraryUnloadOption.Immediately; }
95 }

 

猜你喜欢

转载自www.cnblogs.com/wangxingzhou/p/9226574.html
今日推荐