欢迎来到我的博客
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 @ 2019-04-16 08:03  david96007  阅读(480)  评论(1编辑  收藏  举报