.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CHnage DBText Layer

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
679 Views, 7 Replies

CHnage DBText Layer

I need to create a new layer named "Test" and change the already text layer to "Test" and off the layer "Test". I need to check whether that "Test" layer is existing or not. If not the layer should not be created. Can anyone please help me? Thank you.
7 REPLIES 7
Message 2 of 8
Anonymous
in reply to: Anonymous

"¿Que?"

 

using (Transaction myTrans =dbTransactionManager.StartTransaction())
                    {

LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId,OpenMode.ForRead);

 

LayerTableRecord layerId=db.clayer; 

if (lt.Has("Test"))

{

      layerId= (LayerTableRecord)tr.GetObject(lt["Test"],OpenMode.ForRead);

}

else

{

    lt.UpgradeOpen();

     layerId=new LayerTableRecord();
                lt.Add(layerId);
                layerId.Name = "Test";
                layerId.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);

       tr.AddNewlyCreatedDBObject(layerId,true);

 

}      

...

aEntity.LayerId = layerID



}

Message 3 of 8
Anonymous
in reply to: Anonymous

It is not accepting layerId in the below line, aEntity.LayerId = layerID as layerId should be of ObjectId datatype.
Message 4 of 8
Anonymous
in reply to: Anonymous

Sorry about the error in the previous sample.

 

 

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable tb = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord ms = (BlockTableRecord)tr.GetObject(tb[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);

                LayerTableRecord testLy = null;
                if (lt.Has("Test"))
                {
                    testLy = (LayerTableRecord)tr.GetObject(lt["Test"], OpenMode.ForRead);
                }

                else
                {
                    lt.UpgradeOpen();

                    testLy = new LayerTableRecord();
                    lt.Add(testLy);
                    testLy.Name = "Test";

                    tr.AddNewlyCreatedDBObject(testLy, true);
                }

                //Change CurrentLayer, and thus altering the defaults in "txt.SetDatabaseDefaults()" below
                db.Clayer = testLy.ObjectId;

                DBText txt = new DBText();
                txt.SetDatabaseDefaults();
                ms.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);

                txt.Position = new Point3d(100, 150, 0);
                txt.TextString = "Hello World!";

 

                txt.Layer = "Test"; 
                //or
                txt.LayerId = testLy.ObjectId;
                //or
                txt.LayerId = db.Clayer;

                tr.Commit();
            }

 

 

 

Message 5 of 8
Anonymous
in reply to: Anonymous

Thank you so much for the immediate reply. Layer is getting created and set as current layer in the drawing but i am not able to assign that created layer for the DBText object. In the drawing the DBText already exists and i need to change the existing text layer to Created layer.
Message 6 of 8
Anonymous
in reply to: Anonymous

Now i am able to assign the "Test" layer to the DBText objects but hoe to off that current layer so that the objects in layer "Test" are not visible.
Message 7 of 8
Anonymous
in reply to: Anonymous

Sorry , it is working now. I used the below code, Dim acLyrTbl As LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) Dim sLayerName As String = "Test" Dim acLyrTblRec As LayerTableRecord If acLyrTbl.Has(sLayerName) = False Then acLyrTblRec = New LayerTableRecord() '' Assign the layer a name acLyrTblRec.Name = sLayerName '' Upgrade the Layer table for write acLyrTbl.UpgradeOpen() '' Append the new layer to the Layer table and the transaction acLyrTbl.Add(acLyrTblRec) acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True) Else acLyrTblRec = acTrans.GetObject(acLyrTbl(sLayerName), OpenMode.ForWrite) End If '' Turn the layer off acLyrTblRec.IsOff = True
Message 8 of 8
SENL1362
in reply to: Anonymous

Well done. Its fun learning to understand .NET.

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost