找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1205|回复: 0

[分享] root extension class.

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-9 03:51:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Collections;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using System.Threading;
namespace EAACExtansion
{
    /// <summary>
    /// Constructor for the Document Root object
    /// </summary>
    public abstract class AcExDocumentRoot
    {
        public Document mainAcadDocument;
        public Editor mainEditor;
        public Database mainAcadDatabase;
        public AcExDocumentRoot self;
// I like to enable configuration of my tool tough a textfile. This variables will be used for it.
        private String configPath;
        protected  SortedList<string,string> configuration;
        public String installationPath = "D:\\ACADExtension_2012";
        /// <summary>
        ///
        /// </summary>
        public AcExDocumentRoot()
        {
            self = this;
            try
            {
                mainAcadDocument = Application.DocumentManager.MdiActiveDocument;
                mainEditor = mainAcadDocument.Editor;
                mainAcadDatabase = mainAcadDocument.Database;
            }
            catch ( System.Exception ex) {
                Application.ShowAlertDialog(ex.ToString());
                return;
                }
            configPath = installationPath + "\\Config\\EaAcConfigfile.conf";
            configuration = new SortedList<string,string>();
            // ReReadConfig();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="Question">The question you want the user to answere. This variable will be changed according to the answere of the user.</param>
        /// <returns>the answere false means that the user aborted the question. If true he answered and the question variable is changed.</returns>
        public Boolean AnswereOf(ref String Question)
        {
            PromptStringOptions EditorQuestion = new PromptStringOptions(Question);
            PromptResult Answere;
            do
            {
                Answere = mainEditor.GetString(EditorQuestion);
            } while (Answere.Status != PromptStatus.OK & Answere.Status != PromptStatus.Cancel);
            if (Answere.Status == PromptStatus.Cancel)
            {
                mainEditor.WriteMessage("**Abort**");
            }
            else
            {
                Question = Answere.StringResult;
                return true;
            }
            return false;
        }
        public ObjectId GetLinetypeID(String lineTypeName) {
           LinetypeTable MyLineTypeTable;
           using (Transaction MyTrans = this.mainAcadDocument.TransactionManager.StartTransaction())
           {
               MyLineTypeTable = (LinetypeTable)MyTrans.GetObject(this.mainAcadDatabase.LinetypeTableId, OpenMode.ForRead);
               if (MyLineTypeTable.Has(lineTypeName))
               {
                   return MyLineTypeTable[lineTypeName];
               }
               else {
                   this.mainEditor.WriteMessage("Found wrong spelled linetypename: " + lineTypeName + " in optionfile. Error will occur");
                   throw new System.Exception();
               }
           }
        }
        /// <summary>
        /// This Methode is doing the Operations defined in the Operation objekt. For all Elements in the Document.
        /// </summary>
        /// <param name="MyOperation"></param>
        internal void LoopThrowEachObject(Operation MyOperation)
        {
            long zaehler = 0;
            using (Transaction MyTrans = this.mainAcadDocument.TransactionManager.StartTransaction())
            {
                // Blocktabelle für die Datenbank
                BlockTable MyBlockTable = (BlockTable)MyTrans.GetObject(this.mainAcadDatabase.BlockTableId, OpenMode.ForRead);
                // LinkedList<Thread> Threadlist = new LinkedList<Thread>();
                // Loop über alle Objekte in der Datenbank
                foreach (ObjectId Blocktableobject in MyBlockTable)
                {
                    // Extrahieren der Objekte aus der Tabelle
                    BlockTableRecord DiscreteBlocktableobject = (BlockTableRecord)MyTrans.GetObject(Blocktableobject, OpenMode.ForWrite);
                    foreach (ObjectId DescreteObject in DiscreteBlocktableobject)
                    {
                        // Extrahiere das Entity (also die Linie etc aus der Tabelle für weitere bearbeitung.)
                        Entity MyEntity = (Entity)DescreteObject.GetObject(OpenMode.ForWrite);
                        // Und nun muss man was vernüftiges mit dem Teil tun.
                        // MyOperation.setEntity(MyEntity);
                        MyOperation.start(MyEntity);
                        // M&ouml;gliche Erweiterung um die Funktionalit&auml;t mit Threading.
                        // Thread InstanceCaller = new Thread(MyOperation.start);                        
                        // InstanceCaller.Start(MyEntity);
                        // Threadlist.AddFirst(InstanceCaller);
                        // MyOperation.start(MyEntity);
                        zaehler++;
                        // Und hier hat man hoffentlich mit dem Teil etwas getan.
                    }
                }
                // Nach der Bearbeitung der Element muss man die Transaction übergeben.
                MyTrans.Commit();
            }
        }
        public abstract void CheckExistanceOfSwitches();
        /// <summary>
        ///
        /// </summary>
        /// <param name="newLayer"></param>
        /// <returns> Null if Layer could not added. Else it returns the layer.</returns>
        public LayerTableRecord AddLayer(String newLayer)
        {
            LayerTable myLayerTable;
            LayerTableRecord activeLayer = null;
            // LinetypeTableRecord MyLineTypeTableRevord;
            using (Transaction MyTrans = this.mainAcadDocument.TransactionManager.StartTransaction())
            {
                myLayerTable = (LayerTable)MyTrans.GetObject(this.mainAcadDatabase.LayerTableId, OpenMode.ForRead);
                if (!myLayerTable.Has(newLayer))
                {
                    // try{ activeLayer = (LayerTableRecord) MyTrans.GetObject(myLayerTable[newLayer],OpenMode.ForWrite);
                    try
                    {
                        activeLayer = new LayerTableRecord();
                        activeLayer.Name = newLayer;
                        myLayerTable.Add(activeLayer);
                        MyTrans.Commit();
                        if (myLayerTable.Has(newLayer)) { return activeLayer; }
                    } catch (System.Exception ex) {
                        this.mainEditor.WriteMessage(ex.Message);
                        this.mainEditor.WriteMessage("Layermanager error: " + newLayer + " in this Installation");
                        return null;
                    }  
                }
                if (activeLayer != null) { return activeLayer; }
            }
            this.mainEditor.WriteMessage("Layermanager error: " + newLayer + " in this Installation");
            return null;
        }
        /// <summary>
        /// The function here is to start a operation for each existing layer in the layer table.
        /// </summary>
        /// <param name="myOperation"></param>
        internal void LoopThroughEachLayer(Operation myOperation)
        {
            LayerTable myLayerTable;
            LayerTableRecord activeLayer;
            using (Transaction MyTrans = this.mainAcadDocument.TransactionManager.StartTransaction())
            {
                myLayerTable = (LayerTable)MyTrans.GetObject(this.mainAcadDatabase.LayerTableId, OpenMode.ForRead);
                foreach (ObjectId activeId in myLayerTable)
                {
                    activeLayer = (LayerTableRecord)MyTrans.GetObject(activeId, OpenMode.ForWrite);
                    myOperation.start(activeLayer);
                }
                MyTrans.Commit();
            }
        }
    }
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-22 14:19 , Processed in 0.362367 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表