UG/NX二次開發(fā)(C#)自動刻字三部曲1-打印文本-創(chuàng)新互聯(lián)

自動刻字功能是建模過程中必須用到的,但是網(wǎng)上沒有找到完整的刻字功能實現(xiàn),所以我花了一些時間寫了一套簡易程序,希望能給初學者帶來一點幫助

目前成都創(chuàng)新互聯(lián)已為超過千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)站空間、綿陽服務器托管、企業(yè)網(wǎng)站設計、烏翠網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

先介紹一下建模環(huán)境下刻字功能的實現(xiàn),分三步走(本節(jié)主要介紹打印文本):

第一步:打印文本

第二步:拉伸

第三步:布爾求差

////// 創(chuàng)建文本
        ///////////////public static string TextBuilderByPoint(Point3d p, Tag faceTag , string content)
        {
            Session theSession = Session.GetSession();
            Part workPart = theSession.Parts.Work;
            Part displayPart = theSession.Parts.Display;
            theUFSession = UFSession.GetUFSession();

            int type;
            double[] point = new double[3];  //面的中心點
            double[] dir = new double[3];    //面的法向量
            double[] box = new double[6]; ;
            double radius;
            double rad_data;
            int norm_dir;
            theUFSession.Modl.AskFaceData(faceTag, out type, point, dir, box, out radius, out rad_data, out norm_dir);

            NXOpen.Features.Text nullFeatures_Text = null;
            NXOpen.Features.TextBuilder textBuilder1;
            textBuilder1 = workPart.Features.CreateTextBuilder(nullFeatures_Text);
            Point3d origin1 = new Point3d(0.0, 0.0, 0.0);
            Vector3d normal1 = new Vector3d(0.0, 0.0, 1.0);
            Plane plane1;
            plane1 = workPart.Planes.CreatePlane(origin1, normal1, NXOpen.SmartObject.UpdateOption.WithinModeling);
            textBuilder1.SectionPlane = plane1;
            Unit unit1;
            unit1 = textBuilder1.PlanarFrame.Length.Units;

            textBuilder1.FrameOnPath.AnchorPosition.Expression.RightHandSide = "50";
            textBuilder1.Type = NXOpen.Features.TextBuilder.Types.OnFace;
            textBuilder1.Script = NXOpen.Features.TextBuilder.ScriptOptions.Western;
            textBuilder1.PlanarFrame.AnchorLocation = NXOpen.GeometricUtilities.RectangularFrameBuilder.AnchorLocationType.BottomCenter;
            textBuilder1.PlanarFrame.Length.RightHandSide = "22.169352775646";
            textBuilder1.PlanarFrame.Height.RightHandSide = "10";  //標記高度
            textBuilder1.PlanarFrame.Shear.RightHandSide = "0";
            textBuilder1.OnFacePlacementMethod = NXOpen.Features.TextBuilder.OnFacePlacementMethodOptions.SectionPlane;
            textBuilder1.FrameOnPath.AnchorPosition.Expression.RightHandSide = "50";
            textBuilder1.FrameOnPath.Offset.RightHandSide = "20";
            textBuilder1.FrameOnPath.Length.RightHandSide = "30";  //標記長度
            textBuilder1.FrameOnPath.Height.RightHandSide = "10";
            textBuilder1.FrameOnPath.WScale = 99.9999999999998;
            textBuilder1.SelectFont("Arial", NXOpen.Features.TextBuilder.ScriptOptions.Western);
            //textBuilder1.TextString = DateTime.Now.ToString("yyyy-MM-dd") ;
            textBuilder1.TextString = textBuilder1.Tag.ToString();
            textBuilder1.Type = NXOpen.Features.TextBuilder.Types.Planar;
            textBuilder1.PlanarFrame.Length.RightHandSide = "22.1693527756459";

            //通過創(chuàng)建智能點,確??套衷诿嫔?            Scalar scalar1;
            scalar1 = workPart.Scalars.CreateScalar(0.5, NXOpen.Scalar.DimensionalityType.None, NXOpen.SmartObject.UpdateOption.WithinModeling);
            Scalar scalar2;
            scalar2 = workPart.Scalars.CreateScalar(0.5, NXOpen.Scalar.DimensionalityType.None, NXOpen.SmartObject.UpdateOption.WithinModeling);
            Face face1 = (Face)NXOpen.Utilities.NXObjectManager.Get(faceTag);
            Point point1;
            point1 = workPart.Points.CreatePoint(face1, scalar1, scalar2, NXOpen.SmartObject.UpdateOption.WithinModeling);
            NXObject nXObject1;
            Xform xform1;
            xform1 = workPart.Xforms.CreateExtractXform(face1, NXOpen.SmartObject.UpdateOption.WithinModeling, false, out nXObject1);
            Point3d point2 = new Point3d(p.X, p.Y, p.Z);
            textBuilder1.PlanarFrame.AnchorLocator.SetValue(point1, workPart.ModelingViews.WorkView, point2);

            //顯示智能點的位置
            //Tag point11;
            //double[] Vertex1 = new double[3] { point1.Coordinates.X, point1.Coordinates.Y, point1.Coordinates.Z };
            //theUFSession.Curve.CreatePoint(Vertex1, out point11);

            //更新笛卡爾坐標系,擺正刻字
            CartesianCoordinateSystem cartesianCoordinateSystem1 = (CartesianCoordinateSystem)workPart.FindObject("ENTITY 45 1");
            cartesianCoordinateSystem1.RemoveParameters();
            Point3d origin2 = new Point3d(p.X, p.Y, p.Z);
            cartesianCoordinateSystem1.Origin = origin2;
            Vector3d xDirection1 = new Vector3d(1, 0, 0);
            Vector3d yDirection1 = new Vector3d(0, 1, 0);
            if (dir[2] == 1)
            {
                xDirection1 = new Vector3d(1, 0, 0);
                yDirection1 = new Vector3d(0, 1, 0);
            }
            else if (dir[2] == -1)
            {
                xDirection1 = new Vector3d(1, 0, 0);
                yDirection1 = new Vector3d(0, -1, 0);
            }
            else if (dir[1] == 1)
            {
                xDirection1 = new Vector3d(-1, 0, -0);
                yDirection1 = new Vector3d(-0, -0, 1);
            }
            else if (dir[1] == -1)
            {
                xDirection1 = new Vector3d(1, 0, 0);
                yDirection1 = new Vector3d(0, 0, 1);
            }
            else if (dir[0] == 1)
            {
                xDirection1 = new Vector3d(-0.0, 1.0, -0.0);
                yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
            }
            else if (dir[0] == -1)
            {
                xDirection1 = new Vector3d(0.0, -1.0, 0.0);
                yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
            }
            cartesianCoordinateSystem1.SetDirections(xDirection1, yDirection1);
            textBuilder1.PlanarFrame.UpdateOnCoordinateSystem();

            //顯示笛卡爾坐標系
            //cartesianCoordinateSystem1.SetVisibility(SmartObject.VisibilityOption.Visible);

            ListfeaList = new List();
            feaList = GetFeatureList();

            NXObject nXObject2;
            nXObject2 = textBuilder1.Commit();

            textBuilder1.Destroy();

            foreach (Feature fea in workPart.Features)
            {
                if (!feaList.Contains(fea))
                {
                    Extrude(fea.JournalIdentifier,dir);//執(zhí)行拉伸命令
                    return fea.JournalIdentifier;
                }
            }

            return "";
        }

打印文本的源碼是經(jīng)過錄制修改而來,所以有部分源碼我也還沒完全整明白;

主要實現(xiàn)了NXOpen.Features.TextBuilder這個類來完成文本打印。

其中,需要注意的地方有:

1、要確保文本打印在面上,這里實現(xiàn)了一個智能點

point1 = workPart.Points.CreatePoint(face1, scalar1, scalar2, NXOpen.SmartObject.UpdateOption.WithinModeling);

2、文本在面上之后,怎么擺正文本又是一個問題,所以用到了笛卡爾坐標系

CartesianCoordinateSystem cartesianCoordinateSystem1 = (CartesianCoordinateSystem)workPart.FindObject("ENTITY 45 1");
            cartesianCoordinateSystem1.RemoveParameters();
            Point3d origin2 = new Point3d(p.X, p.Y, p.Z);
            cartesianCoordinateSystem1.Origin = origin2;
            Vector3d xDirection1 = new Vector3d(1, 0, 0);
            Vector3d yDirection1 = new Vector3d(0, 1, 0);
            if (dir[2] == 1)
            {
                xDirection1 = new Vector3d(1, 0, 0);
                yDirection1 = new Vector3d(0, 1, 0);
            }
            else if (dir[2] == -1)
            {
                xDirection1 = new Vector3d(1, 0, 0);
                yDirection1 = new Vector3d(0, -1, 0);
            }
            else if (dir[1] == 1)
            {
                xDirection1 = new Vector3d(-1, 0, -0);
                yDirection1 = new Vector3d(-0, -0, 1);
            }
            else if (dir[1] == -1)
            {
                xDirection1 = new Vector3d(1, 0, 0);
                yDirection1 = new Vector3d(0, 0, 1);
            }
            else if (dir[0] == 1)
            {
                xDirection1 = new Vector3d(-0.0, 1.0, -0.0);
                yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
            }
            else if (dir[0] == -1)
            {
                xDirection1 = new Vector3d(0.0, -1.0, 0.0);
                yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
            }
            cartesianCoordinateSystem1.SetDirections(xDirection1, yDirection1);
            textBuilder1.PlanarFrame.UpdateOnCoordinateSystem();

開發(fā)過程中,大家可以用下面的代碼顯示出笛卡爾坐標系,以輔助開發(fā)cartesianCoordinateSystem1.SetVisibility(SmartObject.VisibilityOption.Visible);

3、這里還有一個大大坑,就是創(chuàng)建的文本沒有返回Tag,所以我這里通過在Commit()方法前后分別獲取所有的特征對象的方式,才獲取到創(chuàng)建的文本

ListfeaList = new List();
            feaList = GetFeatureList();

            NXObject nXObject2;
            nXObject2 = textBuilder1.Commit();

            textBuilder1.Destroy();

            foreach (Feature fea in workPart.Features)
            {
                if (!feaList.Contains(fea))
                {
                    Extrude(fea.JournalIdentifier,dir);//執(zhí)行拉伸命令
                    return fea.JournalIdentifier;
                }
            }

運行結果如圖:

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站標題:UG/NX二次開發(fā)(C#)自動刻字三部曲1-打印文本-創(chuàng)新互聯(lián)
當前網(wǎng)址:http://muchs.cn/article44/ddciee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司動態(tài)網(wǎng)站、域名注冊企業(yè)建站、網(wǎng)站建設、營銷型網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設