Create Layer and Line C#

Create Layer and Line C#

k005
Advisor Advisor
4,170 Views
16 Replies
Message 1 of 17

Create Layer and Line C#

k005
Advisor
Advisor

 

 

Hello

 

I want to create a command that draws Line by Layer.

 

sample:
Layer name: Grobeton
Layer Color: 2

 

osnap : Intersection,MidPoint,EndPoint  = will be marked

 

and I will draw a line that will be active with this layer ... How can I do this?

 

Thanks.

 

0 Likes
Accepted solutions (1)
4,171 Views
16 Replies
Replies (16)
Message 2 of 17

essam-salah
Advisor
Advisor

Hi @k005 

so you want to draw a line in a specific layer? and give it a specific color ?

and you want to programmatically activate 2D Object Snaps: Intersection, MidPoint, EndPoint? 

0 Likes
Message 3 of 17

k005
Advisor
Advisor

 

 

Hi @essam-salah 

 

Yes that's right

0 Likes
Message 4 of 17

essam-salah
Advisor
Advisor

@k005 

so why you don't do it manually: set the current layer to the layer you want and OSnap you want?

what is the point?

give example please

 

0 Likes
Message 5 of 17

k005
Advisor
Advisor

 

 

 

for example :

 

I'll go over an existing drawing. and this will be on the layer I want. osnap, on the other hand, is just to activate object

 

capture for the moment. it is ok even if it is not ..

0 Likes
Message 6 of 17

essam-salah
Advisor
Advisor

@k005 

not clear actually,

so try to give a real example with:

INPUT: ...

OUPUT: ...

0 Likes
Message 7 of 17

essam-salah
Advisor
Advisor
or screen shot
0 Likes
Message 8 of 17

k005
Advisor
Advisor

@essam-salah 

 

 

I don't think screen recording is needed.

 

I just want to do this: create a layer that does not exist in the dwg file (give it color) and draw a line with it. so much.

 

osnap automatically catches the point I mentioned, better if it is activated.

0 Likes
Message 9 of 17

k005
Advisor
Advisor

@essam-salah 

 

Even if the layer is present, it doesn't matter. let it occur again. according to the color I set

0 Likes
Message 10 of 17

essam-salah
Advisor
Advisor

@k005 

got you

Message 11 of 17

essam-salah
Advisor
Advisor

hi @k005 

try this:

using aApp = Autodesk.AutoCAD.ApplicationServices;
[CommandMethod("cieCreateLayer")]
        public static void CreateLayer()
        {
            var doc = aApp.Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var editor = doc.Editor;

            // ask user for layer name
            var promptResult = editor.GetString("Layer Name:");
            if (promptResult.Status != PromptStatus.OK)
            {
                editor.WriteMessage("\nNo String Found\n");
                return;
            }
            string layerName = promptResult.StringResult;

            // set osnap
            aApp.Application.SetSystemVariable("OSMODE", 32); // Intersection
            aApp.Application.SetSystemVariable("OSMODE", 2); // Mid Point
            aApp.Application.SetSystemVariable("OSMODE", 1); // End Point

            using (var ts = db.TransactionManager.StartTransaction())
            {
                // get layers table
                var layersTable = (LayerTable)ts.GetObject(db.LayerTableId, OpenMode.ForWrite);

                // create layer if not exist
                if (layersTable.Has(layerName) == false)
                {
                    var newLayer = new LayerTableRecord();
                    newLayer.Name = layerName;

                    layersTable.Add(newLayer);
                    ts.AddNewlyCreatedDBObject(newLayer, true);
                }

                // set color
                var layer = (LayerTableRecord)layersTable[layerName].GetObject(OpenMode.ForWrite);
                layer.Color = Color.FromRgb(255, 0, 0);
                // set current layer
                db.Clayer = layer.Id;

                // commit changes
                ts.Commit();
            }
        }

 

 

Message 12 of 17

essam-salah
Advisor
Advisor

@k005 

just replace the osnap 3 lines with:

 

// set osnap
aApp.Application.SetSystemVariable("OSMODE", 32 | 2 | 1); // Intersection, Mid Point, End Point

 

just discovered Autodesk.AutoCAD.EditorInput.ObjectSnapMasks is a flags

 

Message 13 of 17

k005
Advisor
Advisor

Hi @essam-salah 

 

 

 

so far...

 

only the command will not exit and the LINE command will run.

 

And it shouldn't ask for the layer name. It should be fixed.

 

So we will enter the layer names in C # code

0 Likes
Message 14 of 17

essam-salah
Advisor
Advisor

@k005 

[CommandMethod("cieCreateLayer")]
        public static void CreateLayer()
        {
            var doc = aApp.Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var editor = doc.Editor;

            string layerName = "Grobeton";

            // set osnap
            aApp.Application.SetSystemVariable("OSMODE", 32 | 2 | 1); // Intersection, Mid Point, End Point            

            using (var ts = db.TransactionManager.StartTransaction())
            {
                // get layers table
                var layersTable = (LayerTable)ts.GetObject(db.LayerTableId, OpenMode.ForWrite);

                // create layer if not exist
                if (layersTable.Has(layerName) == false)
                {
                    var newLayer = new LayerTableRecord();
                    newLayer.Name = layerName;

                    layersTable.Add(newLayer);
                    ts.AddNewlyCreatedDBObject(newLayer, true);
                }

                // set color
                var layer = (LayerTableRecord)layersTable[layerName].GetObject(OpenMode.ForWrite);
                layer.Color = Color.FromRgb(255, 0, 0);
                // set current layer
                db.Clayer = layer.Id;

                // commit changes
                ts.Commit();
            }

            // send Line Cmd
            doc.SendStringToExecute("MULTIPLE LINE ", true, false, false);

        }
0 Likes
Message 15 of 17

essam-salah
Advisor
Advisor

@k005 

have you tried the last code?

 

0 Likes
Message 16 of 17

essam-salah
Advisor
Advisor
Accepted solution

@k005 

 

[CommandMethod("cieCreateLayer")]
        public static void CreateLayer()
        {
            var doc = aApp.Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var editor = doc.Editor;

            string layerName = "Grobeton";

            // set osnap
            aApp.Application.SetSystemVariable("OSMODE", 32 | 2 | 1); // Intersection, Mid Point, End Point            

            using (var ts = db.TransactionManager.StartTransaction())
            {
                // get layers table
                var layersTable = (LayerTable)ts.GetObject(db.LayerTableId, OpenMode.ForWrite);

                // create layer if not exist
                if (layersTable.Has(layerName) == false)
                {
                    var newLayer = new LayerTableRecord();
                    newLayer.Name = layerName;

                    layersTable.Add(newLayer);
                    ts.AddNewlyCreatedDBObject(newLayer, true);
                }

                // set color
                var layer = (LayerTableRecord)layersTable[layerName].GetObject(OpenMode.ForWrite);
                layer.Color = Color.FromRgb(255, 0, 0);
                // set current layer
                db.Clayer = layer.Id;

                // commit changes
                ts.Commit();
            }

            // send Line Cmd
            doc.SendStringToExecute("MULTIPLE LINE ", true, false, false);
        }

 

Message 17 of 17

k005
Advisor
Advisor

@essam-salah 

 

Thank you very much. 🤗

 

I will add a screenshot or a file in my next questions. 🤔

0 Likes