Author Topic: Rename Layer  (Read 4373 times)

0 Members and 1 Guest are viewing this topic.

CadDog01

  • Guest
Rename Layer
« on: January 03, 2011, 11:32:09 AM »
Hi everyone,

I'm new here so go easy...  :-)

I created a Layer Maker Program years ago in VBA but since AutoCAD 2010 doesn't support VBA anymore (without down loading a patch) I thought it was time to more on...

Last year I posted for help to anyone who could help me recreate my program in .net or whatever on AUGI and got great help from Oley which took my VBA code and recreated in C#.
The program works great but one thing I notice a couple of weeks ago.

If the layer is already created and a user changes (let say) the color and later user uses the program to recreate the same layer, It does NOT change the color or style to that the program is layout to do...

In looking at the c# code I found that there isn't a Make or Rename layer there is only a CreateLayer which the program uses.

Here is what comes up in the command line:

A layer with this name already exists.

Here is the code in C#:
Code: [Select]
_layername = dr.Cells[1].Value.ToString();
                    string _linetype = dr.Cells[3].Value.ToString();
                    short _colorindex = short.Parse(dr.Cells[2].Value.ToString());
                    string filename = releasePath + "\\" + "ACAD.LIN";
                    ObjectId id = LayerTools.CreateLayer(_layername, _linetype, _colorindex, filename);

Here is the code in VBA:

Code: [Select]
Private Sub btnApply_Click()
   
    Dim i As Integer
    Dim x As Integer
    Dim layerColl As AcadLayers
    Dim LayerM As AcadLayer
   
           
    i = 0
    x = Me.lstDat.ListCount
    Set layerColl = ThisDrawing.Layers
     
    Do While i < x
        If lstDat.Selected(i) = True Then
                       
            Set LayerM = layerColl.Add(lstDat.List(i, 1))
           
            ThisDrawing.ActiveLayer = LayerM
            LayerM.Color = (lstDat.List(i, 2))
                           
' If Linetype is not loaded then load it
        If LinetypeExist(lstDat.List(i, 3)) = False Then
         On Error Resume Next
          ThisDrawing.Linetypes.Load (lstDat.List(i, 3)), "acad.lin"
                   
        End If
               
' Add the LineType to Layer Name
            ThisDrawing.Linetypes.Add (lstDat.List(i, 3))
            LayerM.Linetype = (lstDat.List(i, 3))
       
        End If
             
        i = i + 1
               
    Loop
ThisDrawing.ActiveLayer = LayerM
Me.Hide
    Call mUnloadQW
End Sub

Here is how I stated on the command line:
Code: [Select]
-layer,m,

etc...

I hope someone can help because I'm very new at C#... :oops:
I would like to be able to overwrite whatever the users changed in the layer

Thanks


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Rename Layer
« Reply #1 on: January 03, 2011, 04:08:53 PM »
First off, Welcome to theSwamp.

Now in order to help, we would need to see the code for ' LayerTools.CreateLayer ', as that is where the magic is happening.  You could either check before that method get called, or you could check within that method if the layer exists.

The simple check is, <LayerTable>.Has( <LayerName> );.  If this returns true, then the layer is there already, and all you need is the LayerTableRecord, and you can change any of the properties that you want.

But the better way, would be to use something like Tony T. posted here
[ http://www.theswamp.org/index.php?topic=12123.msg150999#msg150999 ]

This will only return the ObjectId if the Name exists within the Table, and the record is not erased.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CadDog01

  • Guest
Re: Rename Layer
« Reply #2 on: January 03, 2011, 06:38:41 PM »
Thanks Tim,

Please understand that right now this C# has me like this...  :lol:

While I'm able to edit here and there, Oleg Fattev was the person who wrote this code for me...

*****

I thought I listed the Magic spot... hahahaha

Here are the only options give when you type LayerTools.

--- CreateLayer
--- Equals
--- ReferenceEquals
--- SetLineType

As you can see there isn't a make layer like VBA has
("LayerM"  or  Dim LayerM As AcadLayer "as shown above")
which will overwrite the existing layer.

Right now I'm using SharpDevelop to edit the code and I'm also able to create the dll with it...

I looked at your link but it too got me  :lol:

Again, being new to this, it is going to take me a while to catch up to your statement...  :oops:

I do understand opening the existing layer changing back the color and style to match the standards...
but since you noted that Tony T. method is better I will take a longer look at it and see if I can found it and make it work for my problem...

Thanks a lot for help.
« Last Edit: January 03, 2011, 06:42:31 PM by CadDog01 »

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Rename Layer
« Reply #3 on: January 03, 2011, 10:44:27 PM »
Here is something I had laying around that will create 10 layers.
To show the basics of creating layers.

T.Willey gave you better info for what I think you are trying accomplish, but I can not tell.




Basiclly,

Grab the LayerTable-----This uses Database's property LayerTableId
Start a transaction with a using block
Create a new layerTableRecord
Set the LayerTableRecord Properties
Add LayerTableRecord to LayerTable
Call Transaction.AddNewlyCreatedDBObject ------- to add the LayerTableRecord within the Transaction
Commit the transaction



Code: [Select]

[CommandMethod("CreateLayers")]
        public void CreateLayers()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {               
                    LayerTable lyrTbl = db.LayerTableId.GetObject(OpenMode.ForRead) as LayerTable;
                    for (int i = 1; i < 11; i++)
                    {
                        string lyrName = "LayerColor" + i;
                        if (lyrTbl.Has(lyrName))
                        {
                            continue;
                        }
                        else
                        {
                            lyrTbl.UpgradeOpen();
                            LayerTableRecord lyrTblRecd = new LayerTableRecord();
                            lyrTblRecd.Name = lyrName;
                            lyrTblRecd.Color = Color.FromColorIndex(ColorMethod.ByAci, (short)i);
                            lyrTbl.Add(lyrTblRecd);
                            trx.AddNewlyCreatedDBObject(lyrTblRecd, true);
                            ed.WriteMessage("\nAdded layer " + lyrTblRecd.Name);
                        }                       
                    }             
             
                trx.Commit();
            }           
        }


fixo

  • Guest
Re: Rename Layer
« Reply #4 on: January 04, 2011, 03:34:17 AM »
Thanks Tim,

Please understand that right now this C# has me like this...  :lol:

While I'm able to edit here and there, Oleg Fattev was the person who wrote this code for me...

*****

I thought I listed the Magic spot... hahahaha

Here are the only options give when you type LayerTools.

--- CreateLayer
--- Equals
--- ReferenceEquals
--- SetLineType

As you can see there isn't a make layer like VBA has
("LayerM"  or  Dim LayerM As AcadLayer "as shown above")
which will overwrite the existing layer.

Right now I'm using SharpDevelop to edit the code and I'm also able to create the dll with it...

I looked at your link but it too got me  :lol:

Again, being new to this, it is going to take me a while to catch up to your statement...  :oops:

I do understand opening the existing layer changing back the color and style to match the standards...
but since you noted that Tony T. method is better I will take a longer look at it and see if I can found it and make it work for my problem...

Thanks a lot for help.
Hi, my friend
Please send me this project back, because I've lost them on my machine
That's would be easier and quicker for me to-rewrite it to your needs again

Oleg

CadDog01

  • Guest
Re: Rename Layer
« Reply #5 on: January 04, 2011, 12:55:00 PM »
Hi, my friend
Please send me this project back, because I've lost them on my machine
That's would be easier and quicker for me to-rewrite it to your needs again

Oleg

Thanks again Oleg, since you were not feeling so good I tried working it out myself but found I was totally lost. I went around a few sites until I was told about this site.

I have place the files in my dropbox which I email you the link and password. If I fail in including any file please email me back.

Lastly, the few changes I was able to do by myself were to the look and load command only all else is the same.

Here is the newer smaller look.