Example #1
0
        /// <summary>
        /// 获取属性值
        /// </summary>
        public static string GetAttrValue(this Snap.NX.NXObject nxObject, string title)
        {
            string result = string.Empty;

            if (nxObject.IsHasAttr(title))
            {
                var attr = nxObject.GetAttributeInfo().FirstOrDefault(u => u.Title.ToUpper() == title.ToUpper());
                title = attr.Title;
                switch (attr.Type)
                {
                case Snap.NX.NXObject.AttributeType.Integer:
                {
                    result = nxObject.GetIntegerAttribute(title).ToString();
                    break;
                }

                case Snap.NX.NXObject.AttributeType.Real:
                {
                    result = nxObject.GetRealAttribute(title).ToString();
                    break;
                }

                case Snap.NX.NXObject.AttributeType.String:
                {
                    result = nxObject.GetStringAttribute(title);
                    break;
                }
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// 获取属性值
        /// </summary>
        public static int GetAttrIntegerValue(this Snap.NX.NXObject nxObject, string title)
        {
            int result = 0;

            int.TryParse(nxObject.GetAttrValue(title), out result);
            return(result);
        }
Example #3
0
        public static Snap.NX.NXObject GetViewBorder(EdmDraw.ViewBorderType borderType, NXOpen.Drawings.DraftingView view)
        {
            Snap.NX.NXObject result          = null;
            Snap.Position    viewCenterPoint = view.GetDrawingReferencePoint();
            var viewBorderSize = view.GetBorderSize();

            switch (borderType)
            {
            case EdmDraw.ViewBorderType.Right:
            {
                Snap.Position xLeft = viewCenterPoint;
                xLeft.X += viewBorderSize.X / 2;
                xLeft.Y += viewBorderSize.Y / 2;
                Snap.Position xRight = viewCenterPoint;
                xRight.X += viewBorderSize.X / 2;
                xRight.Y -= viewBorderSize.Y / 2;
                result    = CreateNxObject <Snap.NX.Line>(() => { return(Snap.Create.Line(xLeft, xRight)); }, view.Tag, false);
                break;
            }

            case EdmDraw.ViewBorderType.Top:
            {
                Snap.Position xLeft = viewCenterPoint;
                xLeft.X -= viewBorderSize.X / 2;
                xLeft.Y += viewBorderSize.Y / 2;
                Snap.Position xRight = viewCenterPoint;
                xRight.X += viewBorderSize.X / 2;
                xRight.Y += viewBorderSize.Y / 2;
                result    = CreateNxObject <Snap.NX.Line>(() => { return(Snap.Create.Line(xLeft, xRight)); }, view.Tag, false);
                break;
            }
            }
            return(result);
        }
Example #4
0
        public static string GetAttrValue(Snap.NX.NXObject nxObject, string title)
        {
            string result = string.Empty;

            if (nxObject.GetAttributeInfo().Where(u => u.Title == title).Count() > 0)
            {
                var attr = nxObject.GetAttributeInfo().FirstOrDefault(u => u.Title == title);
                switch (attr.Type)
                {
                case Snap.NX.NXObject.AttributeType.Integer:
                {
                    result = nxObject.GetIntegerAttribute(title).ToString();
                    break;
                }

                case Snap.NX.NXObject.AttributeType.Real:
                {
                    result = nxObject.GetRealAttribute(title).ToString();
                    break;
                }

                case Snap.NX.NXObject.AttributeType.String:
                {
                    result = nxObject.GetStringAttribute(title);
                    break;
                }
                }
            }
            return(result);
        }
Example #5
0
        public static double GetAttrRealValue(this Snap.NX.NXObject nxObject, string title)
        {
            double result = 0.00;

            double.TryParse(nxObject.GetAttrValue(title), out result);
            return(result);
        }
Example #6
0
        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                var theSession = NXOpen.Session.GetSession();
                var workPart   = theSession.Parts.Work;
                var infos      = gridControl1.DataSource as List <JYElecInfo>;
                if (infos != null)
                {
                    infos.ForEach(u =>
                    {
                        string path       = Path.Combine(Path.GetDirectoryName(workPart.FullPath), string.Format("{0}{1}", u.ElecName, Path.GetExtension(workPart.FullPath)));
                        var firstPosition = u.PositioningInfos.FirstOrDefault();
                        var lines         = firstPosition.Lines;
                        var basePoint     = (lines.First() + lines.Last()) / 2;

                        var trans      = Snap.Geom.Transform.CreateTranslation(new Snap.Position() - basePoint);
                        Snap.Vector v1 = new Snap.Vector(0, 0, 1);
                        var faceDir    = firstPosition.FaceDir;
                        var angle      = Snap.Vector.Angle(v1, faceDir);
                        var trans1     = Snap.Geom.Transform.CreateRotation(new Snap.Position(), new Snap.Orientation(faceDir).AxisY, angle);
                        trans          = Snap.Geom.Transform.Composition(trans, trans1);
                        var c          = SnapEx.Create.ExtractObject(firstPosition.NXObjects, path, true, true, trans, basePoint, new Snap.Orientation(faceDir));

                        u.PositioningInfos.ForEach(m =>
                        {
                            m.NXObjects.ForEach(n =>
                            {
                                Snap.NX.NXObject snapN = n;
                                snapN.Delete();
                            });

                            if (m != firstPosition && c != null)
                            {
                                var mLines                     = m.Lines;
                                var mBasePoint                 = (mLines.First() + mLines.Last()) / 2;
                                Snap.NX.Component snapC        = c;
                                Snap.NX.Component newComponent = workPart.ComponentAssembly.CopyComponents(new List <NXOpen.Assemblies.Component> {
                                    c
                                }.ToArray()).First();

                                var transForm = Snap.Geom.Transform.CreateTranslation(mBasePoint - snapC.Position);
                                //平移旋转
                                var tempTrans           = transForm.Matrix;
                                NXOpen.Matrix3x3 matrix = new NXOpen.Matrix3x3();
                                matrix.Xx = tempTrans[0]; matrix.Xy = tempTrans[4]; matrix.Xz = tempTrans[8];
                                matrix.Yx = tempTrans[1]; matrix.Yy = tempTrans[5]; matrix.Yz = tempTrans[9];
                                matrix.Zx = tempTrans[2]; matrix.Zy = tempTrans[6]; matrix.Zz = tempTrans[10];
                                workPart.ComponentAssembly.MoveComponent(newComponent, new NXOpen.Vector3d(tempTrans[3], tempTrans[7], tempTrans[11]), matrix);
                            }
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #7
0
        private static string GetStringAttribute(Snap.NX.NXObject obj, string title)
        {
            var list = obj.GetAttributeInfo().Where(u => u.Title == title && u.Type == Snap.NX.NXObject.AttributeType.String).ToList();

            if (list.Count > 0)
            {
                return(obj.GetStringAttribute(title));
            }
            return(string.Empty);
        }
Example #8
0
        internal static Snap.NX.NXObject CreateNXObject(TaggedObject nxopenTaggedObject)
        {
            Snap.NX.NXObject obj2         = null;
            NXOpen.NXObject  nxopenObject = nxopenTaggedObject as NXOpen.NXObject;
            if (nxopenObject != null)
            {
                obj2 = new Snap.NX.NXObject(nxopenObject);
                NXOpen.Curve nxopenCurve = nxopenObject as NXOpen.Curve;
                if (nxopenCurve != null)
                {
                    obj2 = Snap.NX.Curve.CreateCurve(nxopenCurve);
                }
                switch (GetTypeFromTag(nxopenObject.Tag))
                {
                case ObjectTypes.Type.Point:
                    obj2 = new Snap.NX.Point((NXOpen.Point)nxopenObject);
                    break;

                case ObjectTypes.Type.DatumPlane:
                    obj2 = new Snap.NX.DatumPlane((DatumPlaneFeature)nxopenObject);
                    break;

                case ObjectTypes.Type.DatumAxis:
                    obj2 = new Snap.NX.DatumAxis((DatumAxisFeature)nxopenObject);
                    break;

                case ObjectTypes.Type.CoordinateSystem:
                    obj2 = new Snap.NX.CoordinateSystem((NXOpen.CoordinateSystem)nxopenObject);
                    break;

                case ObjectTypes.Type.Body:
                    obj2 = new Snap.NX.Body((NXOpen.Body)nxopenObject);
                    break;

                case ObjectTypes.Type.Face:
                    obj2 = Snap.NX.Face.CreateFace((NXOpen.Face)nxopenObject);
                    break;

                case ObjectTypes.Type.Edge:
                    obj2 = Snap.NX.Edge.CreateEdge((NXOpen.Edge)nxopenObject);
                    break;

                case ObjectTypes.Type.Feature:
                    obj2 = Snap.NX.Feature.CreateFeature((NXOpen.Features.Feature)nxopenObject);
                    break;

                case ObjectTypes.Type.Component:
                    return(new Snap.NX.Component((NXOpen.Assemblies.Component)nxopenObject));
                }
            }
            return(obj2);
        }
Example #9
0
        public static Snap.Geom.Box3d AcsToWcsBox3d(this Snap.NX.NXObject obj)
        {
            var corners = new List <Snap.Position>();

            obj.Box.Corners.ToList().ForEach(u => {
                var acsOrientation = Snap.Orientation.Identity;
                var wcsOrientation = Snap.Globals.WcsOrientation;
                var transR         = Snap.Geom.Transform.CreateRotation(acsOrientation, wcsOrientation);
                corners.Add(u.Copy(transR));
            });
            var xList = Enumerable.Select(corners, u => u.X).ToList();
            var yList = Enumerable.Select(corners, u => u.Y).ToList();
            var zList = Enumerable.Select(corners, u => u.Z).ToList();

            return(new Snap.Geom.Box3d(xList.Min(), yList.Min(), zList.Min(), xList.Max(), yList.Max(), zList.Max()));
        }
Example #10
0
        public static Snap.NX.NXObject[] FindAllByName(string name)
        {
            Tag @null = Tag.Null;
            List <Snap.NX.NXObject> list = new List <Snap.NX.NXObject>();

            do
            {
                Globals.UFSession.Obj.CycleByName(name, ref @null);
                if (@null != Tag.Null)
                {
                    Snap.NX.NXObject item = CreateNXObject(GetObjectFromTag(@null));
                    list.Add(item);
                }
            }while (@null != Tag.Null);
            return(list.ToArray());
        }
Example #11
0
        private static bool IsBoxable(Snap.NX.NXObject nxObject)
        {
            bool flag = false;

            if (nxObject.ObjectType == ObjectTypes.Type.Line)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Arc)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Conic)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Spline)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Edge)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Face)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Body)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Feature)
            {
                flag = true;
            }
            if (nxObject.ObjectType == ObjectTypes.Type.Component)
            {
                flag = true;
            }
            return(flag);
        }
Example #12
0
 public static Snap.NX.NXObject[] Copy(Transform xform, params Snap.NX.NXObject[] original)
 {
     Snap.NX.NXObject[] objArray = new Snap.NX.NXObject[original.Length];
     for (int i = 0; i < original.Length; i++)
     {
         try
         {
             objArray[i] = original[i].Copy(xform);
         }
         catch (ArgumentException exception)
         {
             if (objArray[i].ObjectType == ObjectTypes.Type.Edge)
             {
                 throw new ArgumentException("One of the input objects is an edge. Edges cannot be copied", exception);
             }
             if (objArray[i].ObjectType == ObjectTypes.Type.Face)
             {
                 throw new ArgumentException("One of the input objects is a face. Faces cannot be copied", exception);
             }
         }
     }
     return(objArray);
 }
Example #13
0
        /// <summary>
        /// 创建探针模型
        /// </summary>
        public static bool CreateProbe(ProbeData data)
        {
            bool result = true;

            try
            {
                Snap.NX.Part basePart = null;
                if (NXOpen.Session.GetSession().Parts.Work == null)
                {
                    var filePath = Path.Combine(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config"), "blank.prt");
                    basePart = Snap.NX.Part.OpenPart(filePath);
                    Snap.Globals.WorkPart = basePart;
                }

                var inPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CMM_INSPECTION");

                //创建路径
                if (!Directory.Exists(inPath))
                {
                    Directory.CreateDirectory(inPath);
                }

                foreach (var ab in data.GetABList())
                {
                    var requireAbBodies = new List <Snap.NX.Body>();
                    var requireUnite    = new List <Snap.NX.Body>();
                    var mark            = Globals.SetUndoMark(Globals.MarkVisibility.Invisible, "CreateProbe");
                    //创建探球
                    var vec    = Snap.Orientation.Identity.AxisZ;
                    var sphere = Snap.Create.Sphere(new Position(), data.D).Body;
                    sphere.Faces.ToList().ForEach(u =>
                    {
                        u.SetStringAttribute(EACTPROBESPHEREFACE, "1");
                    });
                    //创建加长杆
                    var lengtheningRodMaxPosition = new Position(0, 0, data.L);
                    var lengtheningRod            = Snap.Create.Cylinder(new Position(), lengtheningRodMaxPosition, data.d).Body;


                    Action <double, double, double> action = (h, ed, ed2) =>
                    {
                        if (h > 0 && ed > 0)
                        {
                            if (ed2 <= 0)
                            {
                                ed2 = ed;
                            }
                            //创建加长杆1
                            Snap.NX.Body connect1;
                            if (ed != ed2)
                            {
                                connect1 = Snap.Create.Cone(lengtheningRodMaxPosition, vec, new Number[] { ed, ed2 }, h).Body;
                            }
                            else
                            {
                                connect1 = Snap.Create.Cylinder(lengtheningRodMaxPosition, lengtheningRodMaxPosition + new Position(0, 0, h), ed).Body;
                            }

                            lengtheningRodMaxPosition = lengtheningRodMaxPosition + new Position(0, 0, h);
                            requireAbBodies.AddRange(new List <Snap.NX.Body> {
                                connect1
                            });
                            requireUnite.AddRange(new List <Snap.NX.Body> {
                                connect1
                            });
                        }
                    };

                    data.ExtensionBarDataList.ForEach(u => {
                        action(u.Height, u.D1, u.D2);
                    });

                    //创建基座
                    var startPedestal         = lengtheningRodMaxPosition;
                    var firstPedestal         = Snap.Create.Cylinder(startPedestal, startPedestal + new Position(0, 0, data.L2), data.D3).Body;
                    var twoPedestalPosition   = startPedestal + new Position(0, 0, data.L2);
                    var twoPedestal           = Snap.Create.Sphere(twoPedestalPosition, data.D1).Body;
                    var threePedestalPosition = twoPedestalPosition;
                    var threePedestal         = Snap.Create.Cylinder(threePedestalPosition, threePedestalPosition + new Position(0, 0, data.L1), data.D2).Body;

                    //AB旋转
                    requireAbBodies.AddRange(new List <Snap.NX.Body> {
                        sphere, lengtheningRod, firstPedestal
                    });
                    requireUnite.AddRange(new List <Snap.NX.Body> {
                        lengtheningRod, firstPedestal, twoPedestal, threePedestal
                    });
                    var trans = GetTrans(twoPedestalPosition, ab);
                    foreach (var rBody in requireAbBodies)
                    {
                        rBody.Move(trans);
                    }

                    var r = Snap.Create.Unite(sphere, requireUnite.ToArray());
                    r.Orphan();
                    sphere.Move(Snap.Geom.Transform.CreateTranslation(Snap.Position.Origin - Snap.Position.Origin.Copy(trans)));
                    sphere.Name = string.Format("{0}A{1}B{2}", data.ProbeName, ab.A, ab.B);
                    var fileName = Path.Combine(inPath, sphere.Name);
                    if (File.Exists(fileName + ".prt"))
                    {
                        File.Delete(fileName + ".prt");
                    }
                    var dir = Path.GetDirectoryName(fileName);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    //创建干涉检查数据
                    var faces  = sphere.Faces;
                    var points = new List <Position>();
                    faces.ToList().ForEach(u =>
                    {
                        var faceBox     = u.Box;
                        var centerPoint = new Snap.Position((faceBox.MaxX + faceBox.MinX) / 2, (faceBox.MaxY + faceBox.MinY) / 2, (faceBox.MaxZ + faceBox.MinZ) / 2);
                        points.Add(centerPoint);
                        if (!u.IsHasAttr(EACTPROBESPHEREFACE))
                        {
                            u.Edges.ToList().ForEach(e =>
                            {
                                points.Add(e.StartPoint);
                                points.Add(e.EndPoint);
                            });
                        }
                        else
                        {
                            Snap.NX.Face.Sphere sphereFace = u as Snap.NX.Face.Sphere;
                            points.Add(sphereFace.Geometry.Center);
                        }
                    });
                    points = points.Distinct().ToList();
                    var str = Newtonsoft.Json.JsonConvert.SerializeObject(points);
                    sphere.SetStringAttribute(EACT_PROBEINSPECTIONPOINT, str);
                    var exObject = new List <NXOpen.NXObject> {
                        sphere
                    };
                    exObject.ForEach(u => {
                        Snap.NX.NXObject uO = u;
                        uO.IsHidden         = true;
                        uO.SetStringAttribute(SnapEx.ConstString.CMM_INSPECTION_SPHERE, "1");
                    });
                    SnapEx.Create.ExtractObject(exObject, fileName, false, true);
                    Globals.UndoToMark(mark, null);
                }

                if (basePart != null)
                {
                    basePart.Close(true, true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("CreateProbe错误:{0}", ex.Message));
                result = false;
            }
            return(result);
        }
Example #14
0
 public static Snap.Geom.Box3d AcsToWcsBox3d(this Snap.NX.NXObject obj, Snap.Orientation wcs)
 {
     return(AcsToWcsBox3d(obj.Box, wcs));
 }
Example #15
0
 public static bool IsElecMidPoint(Snap.NX.NXObject u, string name)
 {
     return((u is Snap.NX.Point)
            //&& u.Name == SnapEx.EactConstString.EACT_ELECT_MID_POINT
            && GetStringAttribute(u, SnapEx.EactConstString.EACT_ELECT_GROUP) == name);
 }
Example #16
0
 /// <summary>
 /// 是否有该属性
 /// </summary>
 public static bool IsHasAttr(this Snap.NX.NXObject obj, string title)
 {
     return(obj.GetAttributeInfo().Where(m => m.Title.ToUpper() == title.ToUpper()).Count() > 0);
 }
Example #17
0
 /// <summary>
 /// 匹配属性值
 /// </summary>
 public static bool MatchAttrValue(this Snap.NX.NXObject obj, string title, object value)
 {
     return(value.ToString() == obj.GetAttrValue(title));
 }
Example #18
0
        public virtual Snap.NX.NXObject Copy(Transform xform)
        {
            Snap.NX.NXObject obj3;
            UFSession        uFSession = Globals.UFSession;
            int num = 1;

            Tag[] objects = new Tag[] { this.NXOpenTag };
            int   num2    = 2;
            int   num3    = 0;
            int   num4    = 2;

            Tag[] copies = new Tag[num];
            int   status = 0;

            try
            {
                Tag tag;
                uFSession.Trns.TransformObjects(xform.Matrix, objects, ref num, ref num2, ref num3, ref num4, copies, out tag, out status);
                switch (status)
                {
                case 3:
                    throw NXException.Create(0xa4c2d);

                case 4:
                    throw new ArgumentException("The matrix does not preserve angles");

                case 5:
                    throw NXException.Create(0xa3948);

                case 6:
                    throw NXException.Create(0xa3938);

                case 7:
                    throw NXException.Create(0xa3939);

                case 8:
                    throw NXException.Create(0x25);

                case 9:
                    throw NXException.Create(0xa3946);

                case 10:
                    throw NXException.Create(0xa3947);

                case 11:
                    throw NXException.Create(0xa1222);
                }
                if ((status == 0) && (copies[0] == Tag.Null))
                {
                    throw NXException.Create(0xa4c2d);
                }
                Snap.NX.NXObject[] objArray = new Snap.NX.NXObject[num];
                for (int i = 0; i < num; i++)
                {
                    NXOpen.NXObject objectFromTag = GetObjectFromTag(copies[i]);
                    objArray[i] = CreateNXObject(objectFromTag);
                }
                obj3 = objArray[0];
            }
            catch (NXException exception)
            {
                if (this.ObjectType == ObjectTypes.Type.Edge)
                {
                    throw new ArgumentException("The object is an edge. Edges cannot be copied", exception);
                }
                if (this.ObjectType == ObjectTypes.Type.Face)
                {
                    throw new ArgumentException("The object is a face. Faces cannot be copied", exception);
                }
                if (((this.ObjectType == ObjectTypes.Type.Feature) || (this.ObjectType == ObjectTypes.Type.DatumPlane)) || (this.ObjectType == ObjectTypes.Type.DatumAxis))
                {
                    throw new ArgumentException("A feature cannot be copied unless all of its ancestors are copied too", exception);
                }
                if (this.ObjectType == ObjectTypes.Type.CoordinateSystem)
                {
                    throw new ArgumentException("A transform that does not preserve angles cannot be applied to a coordinate system");
                }
                throw exception;
            }
            return(obj3);
        }
Example #19
0
        public static Snap.Position GetCenterPoint(this Snap.NX.NXObject obj)
        {
            var box = obj.Box;

            return(new Snap.Position((box.MinX + box.MaxX) / 2, (box.MinY + box.MaxY) / 2, (box.MinZ + box.MaxZ) / 2));
        }
Example #20
0
 public static double GetDoubleAttr(Snap.NX.NXObject nxObject, string title)
 {
     return(ConvertToDouble(GetAttrValue(nxObject, title)));
 }