欢迎来到我的博客
Civil 3D开发与应用,欢迎加入QQ群:484124761
AutoCAD开发,欢迎加入QQ群:193522571

Civil 3D 二次开发 名称模板不能正常工作

using Autodesk.AECC.Interop.Land;
using Autodesk.AECC.Interop.UiLand;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.Settings;
[assembly: CommandClass(typeof(NameTemplate.NameTemplateTest))]
namespace NameTemplate
{
    class NameTemplateTest
    {
        string m_sAeccAppProgId = "AeccXUiLand.AeccApplication.11.0";
        protected Document doc;
        protected Database db;
        protected Editor ed;
        protected CivilDocument civilDoc;
        ObjectId alignmentId;
        ObjectId samplineGroupId;

        private IAcadApplication m_oAcadApp = null;
        private IAeccApplication m_oAeccApp = null;
        private IAeccDocument m_oAeccDoc = null;
        private IAeccDatabase m_oAeccDb = null;

        AeccAlignment comAlignment;
        AeccSampleLineGroup comSLG;


        public NameTemplateTest()
        {
            doc = Application.DocumentManager.MdiActiveDocument;
            ed = doc.Editor;
            db = doc.Database;
            civilDoc = CivilApplication.ActiveDocument;

            m_oAcadApp = (IAcadApplication)Application.AcadApplication;
            if (m_oAcadApp != null)
            {
                m_oAeccApp = (IAeccApplication)m_oAcadApp.GetInterfaceObject(m_sAeccAppProgId);
                m_oAeccDoc = (IAeccDocument)m_oAeccApp.ActiveDocument;
                m_oAeccDb = (IAeccDatabase)m_oAeccApp.ActiveDocument.Database;
            }
        }
        public void ComNameTemplateTest()
        {
            if (m_oAeccDoc == null) return;
            string name = m_oAeccDoc.Settings.AlignmentSettings.NameTemplate.Value;
            object alignmentStyle = m_oAeccDoc.AlignmentStyles[0];
            object alignmentLadelset = m_oAeccDoc.AlignmentLabelStyleSets[0];
            string slgName = m_oAeccDoc.Settings.SampleLineSettings.GroupNameTemplate.Value;
            AeccGroupPlotStyle GroupPlotStyle = m_oAeccDoc.GroupPlotStyles[0];
            AeccSampleLineStyle SampleLineStyle = m_oAeccDoc.SampleLineStyles[0];
            AeccLabelStyle SampleLineLabelStyle = m_oAeccDoc.SampleLineLabelStyles[0];
            string slName = m_oAeccDoc.Settings.SampleLineSettings.NameTemplate.Value;
            double[] sp = new double[] { 0, 0, 0 };
            double[] ep = new double[] { 200, 0, 0 };

            for (int i = 0; i < 3; i++)
            {
                comAlignment = m_oAeccDoc.AlignmentsSiteless.Add(
                    name, "0", alignmentStyle, alignmentLadelset);
                comAlignment.Entities.AddFixedLine1(sp,ep);
                for (int j = 0; j < 3; j++)
                {
                    comSLG = comAlignment.SampleLineGroups.Add(
                        slgName, "0", GroupPlotStyle, SampleLineStyle, SampleLineLabelStyle);

                    double[] stations = new double[] { 0, 10, 20, 30, 40 };

                    foreach (double sta in stations)
                    {
                        comSLG.SampleLines.AddByStation(slName, sta, 20, 20);
                    }
                }
            }

        }

        public void CreateSurfaceTest()
        {
            string name = civilDoc.Settings.GetSettings<
                SettingsCmdCreateSurface>().NameFormat.Surface.Value;
            //Create 3 TinSurface
            for (int i = 0; i < 3; i++)
            {
                TinSurface.Create(db, name);
            }
        }
        public void CreateSampleLineGroupTest()
        {
            if (alignmentId == ObjectId.Null) return;
            try
            {

                    string name = civilDoc.Settings.GetSettings<
                        SettingsSampleLine>().NameFormat.SampleLineGroup.Value;
                    //Create 3 SampleLineGroup
                    for (int i = 0; i < 3; i++)
                    {
                        samplineGroupId = SampleLineGroup.Create(name, alignmentId);
                    }
 
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\n" + ex.Message);
            }
        }
        public void CreateSampleLineTest()
        {
            if (samplineGroupId == ObjectId.Null) return;
            try
            {

                string name = civilDoc.Settings.GetSettings<
                    SettingsSampleLine>().NameFormat.SampleLine.Value;
                double[] stations = new double[] { 0, 10, 20, 30, 40 };

                foreach (double sta in stations)
                {
                    SampleLine.Create(name, samplineGroupId, sta);
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\n" + ex.Message);
            }
        }


        public void CreateAlignmentTest()
        {
            try
            {
                string name = civilDoc.Settings.GetSettings<
                    SettingsCmdCreateAlignmentLayout>()
                    .DefaultNameFormat.AlignmentNameTemplate.Value;
                ObjectId layerId;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    LayerTable layertable = db.LayerTableId.GetObject(
                        OpenMode.ForRead) as LayerTable;
                    layerId = layertable["0"];
                    tr.Commit();
                }
                ObjectId styleId = civilDoc.Styles.AlignmentStyles[0];
                ObjectId labelsetId = civilDoc.Styles.LabelSetStyles
                    .AlignmentLabelSetStyles[0];
                for (int i = 0; i < 3; i++)
                {
                    alignmentId= Alignment.Create(
                        civilDoc, name, ObjectId.Null, layerId, styleId, labelsetId);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        Alignment al = alignmentId.GetObject(OpenMode.ForWrite) as Alignment;
                        al.Entities.AddFixedLine(new Point3d(0, 0, 0), new Point3d(0, 200, 0));
                        tr.Commit();
                    }
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\n" + ex.Message);
            }
        }


        [CommandMethod("ComTest")]
        public void ComTest() 
        {
            NameTemplateTest ntt = new NameTemplateTest();
            ntt.ComNameTemplateTest();
        }
        [CommandMethod("NetTest")]
        public void NetTest()
        {
            NameTemplateTest ntt = new NameTemplateTest();
            ntt.CreateSurfaceTest();
            ntt.CreateAlignmentTest();
            ntt.CreateSampleLineGroupTest();
            ntt.CreateSampleLineTest();

        }
    }
}

使用NET API,曲面名称模板可以正常工作,采样线和路线名称模板不能正常工作。

 使用COM API 路线的采样线名称模板正常工作,采样线编组不行。





 

 

 

posted @ 2017-06-23 13:03  david96007  阅读(1007)  评论(0编辑  收藏  举报