Author Topic: Create new layer by selecting a single object  (Read 1894 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Create new layer by selecting a single object
« on: May 12, 2013, 09:03:47 PM »
      This code example about how to create a new layer that has same properties as the layer that already exist in a drawing,
      by select single entity on existing this layer and then copy properties from

       
Code: [Select]

       [CommandMethod("CopyLayerFromObject")]

        public void testCopyLayer()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            PromptStringOptions sopts = new PromptStringOptions("\nEnter new layer name :");

            sopts.AllowSpaces = true;

            PromptResult stres = ed.GetString(sopts);

            if (stres.Status != PromptStatus.OK) return;
            // get new layer name
            string layerName = stres.StringResult;

            PromptSelectionOptions opts = new PromptSelectionOptions();

            opts.MessageForRemoval = "Wrong selection, try again.";

            opts.MessageForAdding = "Select single object to copy layer from: ";

            opts.SingleOnly = true;

            opts.SinglePickInSpace = true;
            // select single object
            PromptSelectionResult selRes = ed.GetSelection(opts);

            if (selRes.Status != PromptStatus.OK) return;

            ObjectId entId = selRes.Value.GetObjectIds()[0];

            // open transaction

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                LayerTable ltb = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                // get entity
                Entity ent = (Entity)tr.GetObject(entId, OpenMode.ForRead);
                // get entity layer
                string oldname = ent.Layer;
                // get LayerTableRecord by name
                LayerTableRecord oldLtr = (LayerTableRecord)tr.GetObject(ltb[oldname], OpenMode.ForRead);
                // check if layer exists
                if (!ltb.Has(layerName))
                {
                    ltb.UpgradeOpen();

                    //create new layer
                    LayerTableRecord ltr = new LayerTableRecord();
                    // copy properties from entity layer
                    ltr.CopyFrom(oldLtr);
                    // add new layer name
                    ltr.Name = layerName;
                    // here you can to add other properties to a layer: linetype, color etc
                    //
                    // add layer to LayerTable
                    ltb.Add(ltr);
                    // confirm transaction
                    tr.AddNewlyCreatedDBObject(ltr, true);

                }
                // end command if layer exist
                else
                {
                    return;
                }

                tr.Commit();

            }//end using transaction

        }

   VB.NET
Code: [Select]
        <CommandMethod("CopyLayerFromObject")> _
        Public Sub testCopyLayer()

            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim db As Database = doc.Database

            Dim ed As Editor = doc.Editor

            Dim sopts As New PromptStringOptions(vbLf & "Enter new layer name :")

            sopts.AllowSpaces = True

            Dim stres As PromptResult = ed.GetString(sopts)

            If stres.Status <> PromptStatus.OK Then
                Return
            End If
            ' get new layer name
            Dim layerName As String = stres.StringResult

            Dim opts As New PromptSelectionOptions()

            opts.MessageForRemoval = "Wrong selection, try again."

            opts.MessageForAdding = "Select single object to copy layer from: "

            opts.SingleOnly = True

            opts.SinglePickInSpace = True
            ' select single object
            Dim selRes As PromptSelectionResult = ed.GetSelection(opts)

            If selRes.Status <> PromptStatus.OK Then
                Return
            End If

            Dim entId As ObjectId = selRes.Value.GetObjectIds()(0)

            ' open transaction

            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim ltb As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable)
                ' get entity
                Dim ent As Entity = DirectCast(tr.GetObject(entId, OpenMode.ForRead), Entity)
                ' get entity layer
                Dim oldname As String = ent.Layer
                ' get LayerTableRecord by name
                Dim oldLtr As LayerTableRecord = DirectCast(tr.GetObject(ltb(oldname), OpenMode.ForRead), LayerTableRecord)
                ' check if layer exists
                If Not ltb.Has(layerName) Then
                    ltb.UpgradeOpen()

                    'create new layer
                    Dim ltr As New LayerTableRecord()
                    ' copy properties from entity layer
                    ltr.CopyFrom(oldLtr)
                    ' add new layer name
                    ltr.Name = layerName
                    ' here you can to add other properties to a layer: linetype, color etc
                    '
                    ' add layer to LayerTable
                    ltb.Add(ltr)
                    ' confirm transaction

                    tr.AddNewlyCreatedDBObject(ltr, True)
                Else
                    ' end command if layer exist
                    Return
                End If

                tr.Commit()
            End Using
            'end using transaction
        End Sub