change layer.color and layer.line weight by int and double

change layer.color and layer.line weight by int and double

Anonymous
Not applicable
1,981 Views
4 Replies
Message 1 of 5

change layer.color and layer.line weight by int and double

Anonymous
Not applicable

i am make function in net autocad using C#

i want change layer.color by int and change layer.lineweight by double

how to do this

 public static ObjectId addlayer(string namalayer,double lineweight,int color)

        {
            var doc = app.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            ObjectId idx = db.LayerTableId;
            using (Transaction acTrans = db.TransactionManager.StartTransaction())
            {
                LayerTable acLyrTbl;
                acLyrTbl = acTrans.GetObject(db.LayerTableId,OpenMode.ForRead) as LayerTable;  
                if (acLyrTbl.Has(namalayer) == false)
                {
                    LayerTableRecord acLyrTblRec = new LayerTableRecord();
                    acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByColor, color);
                    acLyrTblRec.LineWeight = lineweight;


                    acLyrTblRec.Name = namalayer;
                    acLyrTbl.UpgradeOpen();
                    idx= acLyrTbl.Add(acLyrTblRec);acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);   
                    db.Clayer = idx;
                }
                else
                {
                    db.Clayer = acLyrTbl[namalayer];
                }              
                acTrans.Commit();
                return idx;
            }

        }
0 Likes
Accepted solutions (1)
1,982 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

About color, to pass an integer as color index, you have to use the ColorMethod.ByAci.

About line weight, you have to use a member of the LineWeight enumeration or its corresponding integer.

 

public static ObjectId AddLayer(string name, double lineweight, int color)
{
    var doc = app.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    ObjectId idx;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
        if (layerTable.Has(name))
        {
            idx = layerTable[name];
        }
        else
        {
            var layer = new LayerTableRecord();
            layer.Color = Color.FromColorIndex(ColorMethod.ByAci, (short)color);
            try { layer.LineWeight = (LineWeight)(int)(lineweight * 100); }
            catch { layer.LineWeight = LineWeight.ByLayer; }
            layer.Name = name;
            layerTable.UpgradeOpen();
            idx = layerTable.Add(layer); 
            tr.AddNewlyCreatedDBObject(layer, true);
        }
            db.Clayer = idx;
        tr.Commit();
        return idx;
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

Anonymous
Not applicable

@_gile  i has try 

Objectid idx; (but while compile allert idx unlocal variable) how to fix

0 Likes
Message 4 of 5

_gile
Consultant
Consultant

@Anonymous  a écrit :

@_gile  i has try 

Objectid idx; (but while compile allert idx unlocal variable) how to fix


Sorry but I do not understand what you mean. The code I posted works as expected here.

Either you copy the whole code or you only adapt the Color and LineWeight expressions to your code.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

Anonymous
Not applicable

@_gile  thankyou work like charm

 

0 Likes