2012년 11월 23일 금요일

[코드]FreezeDoorLayer 샘플소스(postID=8919245894159416811)

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Interop;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using WinApp = System.Windows.Forms.Application;

public class Script
{
    private ObjectId _idField;
    private ObjectId _idFrom;
    private ObjectId _idTo;

    [CommandMethod("FreezeDoorLayer")]
    public static void FreezeDoorLayer()
    {
        Document acDoc = AcadApp.DocumentManager.MdiActiveDocument;
        Database acCurDb = acDoc.Database;
        // Start a transaction
        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            LayerTable acLyrTbl;
            acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
            foreach (ObjectId acObjId in acLyrTbl)
            {
                LayerTableRecord acLyrTblRec;
                acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead) as LayerTableRecord;
                if (acLyrTblRec.Name.StartsWith("Door", StringComparison.OrdinalIgnoreCase) == true)
                {
                    if (acLyrTblRec.ObjectId != acCurDb.Clayer)
                    {
                        acLyrTblRec.UpgradeOpen();
                        acLyrTblRec.IsFrozen = true;
                    }
                }
            }
            // Commit the changes and dispose of the transaction
            acTrans.Commit();
        }
    }

    public string Open(string hField, string hFrom, string hTo)
    {
        Document doc = AcadApp.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        try
        {
            Database db = doc.Database;

            long ln1 = Convert.ToInt64(hField, 16);
            Handle hn1 = new Handle(ln1);
            _idField = db.GetObjectId(false, hn1, 0);

            long ln2 = Convert.ToInt64(hFrom, 16);
            if (ln2 > 0)
            {
                Handle hn2 = new Handle(ln2);
                _idFrom = db.GetObjectId(false, hn2, 0);
                RegisterEvent();
            }

            long ln3 = Convert.ToInt64(hTo, 16);
            if (ln3 > 0)
            {
                Handle hn3 = new Handle(ln3);
                _idTo = db.GetObjectId(false, hn3, 0);
            }
        }
        catch (Autodesk.AutoCAD.Runtime.Exception e)
        {
            return e.Message;
        }
        return Regen(hField, hFrom, hTo);
    }

    public bool RegisterEvent()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            Entity fromEntity = tr.GetObject(_idFrom, OpenMode.ForWrite, false) as Entity;
            fromEntity.Modified += new EventHandler(SendCmdFromProp);
            tr.Commit();
        }
        return true;
    }

    public bool UnRegisterEvent()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            Entity fromEntity = tr.GetObject(_idFrom, OpenMode.ForWrite, false) as Entity;
            fromEntity.Modified -= new EventHandler(SendCmdFromProp);
            tr.Commit();
        }
        return true;
    }

    public void SendCmdFromProp(object senderObj, EventArgs evtArgs)
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            DBObject obj = tr.GetObject(_idFrom, OpenMode.ForRead);
            if (obj is BlockReference)
            {
                BlockReference blkRef = obj as BlockReference;
                if (blkRef.IsDynamicBlock)
                {
                    DynamicBlockReferencePropertyCollection pp = blkRef.DynamicBlockReferencePropertyCollection;
                    foreach (DynamicBlockReferenceProperty prop in pp)
                    {
                        doc.SendStringToExecute(prop.Value + " ", false, false, true);
                    }
                }
            }
            tr.Commit();
        }
    }

    public string Regen(string hField, string hFrom, string hTo)
    {
        return "명령어 선택 = ";
    }
}

댓글 1개:

  1. 1. autocad 2013을 구동합니다.
    2. field명령어를 실행합니다.
    3. postID를 입력합니다.
    4. 닫기한 후에 "Regen"으로 dotnet 코드를 로드시킵니다.

    답글삭제