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

纵断面图标注栏数据复制

新西兰本地化包中有一项小功能不错——纵断面图标注栏数据复制

刚好这几天我们自己也遇到了同样的需求:

利用纵断面来创建场地剖面,

剖面图标注栏要进行重复的设置。

于是参照新西兰本地化包中的功能,

自己也写了一遍。

但写的过程中遇到了一些问题,

在遇到问题后,

没有第一时间想起查看api reference,

没有查看每个属性是否会抛出异常……

导致冒昧的向Autodesk负责API接口开发的丁工直接请教,

实在是尴尬……

 

相关代码如下:

复制代码
    class SetProfileViewDataband
    {
        Document doc;
        Database db;
        Editor ed;
        CivilDocument cDoc;

        ObjectId fillShapeStyleId;
        ObjectId cutShapeStyleId;
        public SetProfileViewDataband()
        {
            doc = Application.DocumentManager.MdiActiveDocument;
            db = doc.Database;
            ed = doc.Editor;
            cDoc = CivilApplication.ActiveDocument;

        }
复制代码

 

 

复制代码
 /// <summary>
        /// 2019年04月15日
        /// 按照新西兰本地化包中的功能,
        /// 进行纵断面数据复制
        /// </summary>
        public void ProfileDataBandCopy()
        {

            PromptEntityOptions peo = new PromptEntityOptions("\n选择源纵断面图: ");
            peo.SetRejectMessage("只允许选择一个纵断面图. \n");
            peo.AddAllowedClass(typeof(ProfileView), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                ProfileView view;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    view = per.ObjectId.GetObject(OpenMode.ForRead) as ProfileView;
                    tr.Commit();
                }
                ProfileViewBandItemCollection bottomBandItems = view.Bands.GetBottomBandItems();
                ProfileViewBandItemCollection topBandItems = view.Bands.GetTopBandItems();
                TypedValue[] tv = new TypedValue[] { new TypedValue(0, "AECC_PROFILE_VIEW") };

                SelectionFilter filter = new SelectionFilter(tv);
                PromptSelectionResult psr = ed.GetSelection(filter);
                if (psr.Status == PromptStatus.OK)
                {
                    foreach (ObjectId id in psr.Value.GetObjectIds())
                    {
                        using (Transaction tr2 = db.TransactionManager.StartTransaction())
                        {
                            ProfileView view2 = tr2.GetObject(id, OpenMode.ForWrite) as ProfileView;

                            ProfileViewBandItemCollection bandItems = view2.Bands.GetBottomBandItems();
                            ProfileViewBandItemCollection items4 = view2.Bands.GetTopBandItems();
                            bandItems = new ProfileViewBandItemCollection(view2.ObjectId, BandLocationType.Bottom);
                            items4 = new ProfileViewBandItemCollection(view2.ObjectId, BandLocationType.Top);
                            this.AddDataBandItems(bottomBandItems, ref bandItems, db);
                            this.AddDataBandItems(topBandItems, ref items4, db);
                            if (bandItems.Count() > 0)
                            {
                                view2.Bands.SetBottomBandItems(bandItems);
                            }
                            if (items4.Count() > 0)
                            {
                                view2.Bands.SetTopBandItems(items4);
                            }
                            tr2.Commit();
                        }
                    }
                }
            }
        }
复制代码

 

 

复制代码
  public void AddDataBandItems(ProfileViewBandItemCollection srcBandItems,
            ref ProfileViewBandItemCollection targetBandItems, Database db)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                for (int i = 0; i < srcBandItems.Count(); i++)
                {
                    ProfileViewBandItem item = srcBandItems[i];
                    targetBandItems.Add(item.BandStyleId);

                    Type type = item.GetType();
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo prop in properties)
                    {
                        if (prop.CanWrite)
                        {
                            //如果不加try,不少属性值无法读取到,
                            //具体哪些属性会抛出异常,可以在api reference中查看
                            try
                            {
                                object v = prop.GetValue(item);
                                prop.SetValue(targetBandItems[i], v);
                            }
                            catch 
                            {
                            }
                        }
                    }
                }
                tr.Commit();
            }
        }
复制代码

 

 

总结:遇到问题第一个想到应该是去查看api reference,看是否会抛出异常。

 

posted @   david96007  阅读(505)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2018-04-16 转 Debugging AutoCAD 2017 using Visual Studio 2015
点击右上角即可分享
微信分享提示