.NET

Reply
Valued Contributor
kob4lt
Posts: 61
Registered: ‎04-14-2010
Message 1 of 14 (788 Views)
Accepted Solution

layers in new database

788 Views, 13 Replies
10-09-2010 06:01 AM

Can I create a new layer and then add a few entities in that layers in newly created database?

 

Somethink like this

 

 

private void DodavanjePoligona(ref Database newDB, string imeLayera, Point2dCollection tockeCestica)
        {
            Random random = new Random();
            //using (DocumentLock doclock = doc.LockDocument())
            //{
                using (Transaction trans = newDB.TransactionManager.StartTransaction())
                {
                    LayerTable layerKolekcija = trans.GetObject(newDB.LayerTableId, OpenMode.ForRead) as LayerTable;
                    if (layerKolekcija.Has(imeLayera) == false)
                    {
                        LayerTableRecord ltr = new LayerTableRecord();
                        ltr.Color = Color.FromRgb((byte)random.Next(255), (byte)random.Next(255), (byte)random.Next(255));
                        ltr.Name = imeLayera;
                        layerKolekcija.UpgradeOpen();
                        layerKolekcija.Add(ltr);
                        trans.AddNewlyCreatedDBObject(ltr, true);
                    }
                    BlockTableRecord btr = trans.GetObject(newDB.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                    Polyline cestica = new Polyline();
                    int count = 0;
                    foreach (Point2d toc in tockeCestica)
                    {
                        cestica.AddVertexAt(count, toc, 0, 0, 0);
                        count++;
                    }
                    try
                    {
                        cestica.Layer = imeLayera;
                    }
                    catch (Exception ex)
                    {
                    }
                    btr.AppendEntity(cestica);
                    trans.AddNewlyCreatedDBObject(cestica, true);
                    trans.Commit();
                }
            //}
        }

It crashes at try block...

 

But when I save newDB and open it, I can see that new layers has been created 

 

I had this problem as well, Not sure why. I noticed that if I set the layer current with Dababase.Clayer and just added entites, they went into the new layer ok.

Valued Mentor
KerryBrown
Posts: 269
Registered: ‎11-29-2008
Message 2 of 14 (773 Views)

Re: layers in new database

10-09-2010 03:19 PM in reply to: kob4lt

 

How are you calling this ??

Can you post the calling code including the  CommandMethod Attribute Statement

//-------------------------------------------------------

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Mentor
KerryBrown
Posts: 269
Registered: ‎11-29-2008
Message 3 of 14 (768 Views)

Re: layers in new database

10-09-2010 04:43 PM in reply to: KerryBrown

 

Just for fun - current document ;

 

[CommandMethod("Test101001")]

public void Test101001()
{
    Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;
    string layerName;

    Point2dCollection pts = new Point2dCollection();
    pts.Add(new Point2d(0.0, 0.0));
    pts.Add(new Point2d(150.0, 0.0));
    pts.Add(new Point2d(150.0, 100.0));
    pts.Add(new Point2d(0.0, 200.0));
    layerName = "TestLayer_1";
    DodavanjePoligona(ref db, layerName, pts);

    pts.Clear();

    pts.Add(new Point2d(20.0, 20.0));
    pts.Add(new Point2d(170.0, 20.0));
    pts.Add(new Point2d(170.0, 120.0));
    pts.Add(new Point2d(20.0, 220.0));
    layerName = "TestLayer_2";
    DodavanjePoligona(ref db, layerName, pts);

    AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDone: ");
}

 and - new Document ;

 

[CommandMethod("Test101003"CommandFlags.Session)]

public void Test101003()
{
    // Create a new document database
    DocumentCollection docMgr = Application.DocumentManager;
    Document acNewDoc = docMgr.Add("acadiso.dwt");
    Database acDbNewDoc = acNewDoc.Database;

    string layerName;
    Point2dCollection pts = new Point2dCollection();
    pts.Add(new Point2d(0.0, 0.0));
    pts.Add(new Point2d(150.0, 0.0));
    pts.Add(new Point2d(150.0, 100.0));
    pts.Add(new Point2d(0.0, 200.0));
    layerName = "TestLayer_1";

    // Lock the new document
    using (DocumentLock acDocLock = acNewDoc.LockDocument())
    {
        DodavanjePoligona(ref acDbNewDoc, layerName, pts);
    } // Unlock the document

    pts.Clear();
    pts.Add(new Point2d(20.0, 20.0));
    pts.Add(new Point2d(170.0, 20.0));
    pts.Add(new Point2d(170.0, 120.0));
    pts.Add(new Point2d(20.0, 220.0));
    layerName = "TestLayer_2";

    // Lock the new document
    using (DocumentLock acDocLock = acNewDoc.LockDocument())
    {
        DodavanjePoligona(ref acDbNewDoc, layerName, pts);

        pts.Clear();
        pts.Add(new Point2d(40.0, 40.0));
        pts.Add(new Point2d(190.0, 40.0));
        pts.Add(new Point2d(190.0, 140.0));
        pts.Add(new Point2d(40.0, 240.0));
        layerName = "TestLayer_3";
        DodavanjePoligona(ref acDbNewDoc, layerName, pts);

    } // Unlock the document


    // Set the new document current
    docMgr.MdiActiveDocument = acNewDoc;

}
//-------------------------------------------------------

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Mentor
KerryBrown
Posts: 269
Registered: ‎11-29-2008
Message 4 of 14 (763 Views)

Re: layers in new database

10-09-2010 05:16 PM in reply to: KerryBrown

 

Just a side note:

 

You may be best to add a functional statement to the catch rather than leave it empty.

//

 

Random objects that are instantiated in close succession generate an identical series of random numbers ...

So,

If you really want random colors for your layers it may be best to generate the Random object once in the calling routine and re-use it for each call to the DodavanjePoligona

 .

 

//-------------------------------------------------------

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Contributor
kob4lt
Posts: 61
Registered: ‎04-14-2010
Message 5 of 14 (695 Views)

Re: layers in new database

10-12-2010 05:19 AM in reply to: KerryBrown

 


KerryBrown wrote:

 

How are you calling this ??

Can you post the calling code including the  CommandMethod Attribute Statement


 

This method is calling from a button on form and form is calling like this

 

[CommandMethod("energo")]
        public void energo()
        {
            EnergoForma FormaEnergometana = new EnergoForma();
            FormaEnergometana.Show();
        }

 and the calling method

 

DodavanjePoligona(ref newDB,opcina, kolekcijaTockakaCestice);

 

 

 

 

 

Valued Mentor
KerryBrown
Posts: 269
Registered: ‎11-29-2008
Message 6 of 14 (674 Views)

Re: layers in new database

10-12-2010 04:13 PM in reply to: kob4lt

 

What you've posted is very little use for me to help you.

 

Did the code I posted help at all. ?

Are you locking the document ?

Did you try using

[CommandMethod("myCommand"CommandFlags.Session)]

//-------------------------------------------------------

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Mentor
Jeffrey_H
Posts: 372
Registered: ‎01-20-2010
Message 7 of 14 (668 Views)

Re: layers in new database

10-12-2010 04:44 PM in reply to: KerryBrown

Also I noticed and if Kerry agree's

 

 you should consider using 

 

Application.ShowModelessDialog()

or

 

Application.ShowModalDialog()

 

You can also find your answers @ TheSwamp
Valued Mentor
KerryBrown
Posts: 269
Registered: ‎11-29-2008
Message 8 of 14 (617 Views)

Re: layers in new database

10-15-2010 05:23 PM in reply to: kob4lt

 

 

Helllloooooo ...

Anyone home ??

//-------------------------------------------------------

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Contributor
kob4lt
Posts: 61
Registered: ‎04-14-2010
Message 9 of 14 (602 Views)

Re: layers in new database

10-16-2010 04:45 PM in reply to: KerryBrown

Sorry, I am on a trip right now... I will reply to you as soon as posible

Valued Contributor
kob4lt
Posts: 61
Registered: ‎04-14-2010
Message 10 of 14 (506 Views)

Re: layers in new database

11-01-2010 02:08 AM in reply to: kob4lt

Sorry for my delay of answering to you all.

 

So, here is new code and the same thing is happing in this code

 

 

void UbacivanjeObjekataUDB(object[] podaciOObjektu)
        {
            ObjectId idBlocka;
            Point2d tempTocka;
            BlockReference block;
            string data;
            string data1;
            Random random = new Random();

            string feature = podaciOObjektu.GetValue(0).ToString();
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction trans = newDB.TransactionManager.StartTransaction())
                {
                    BlockTable bt = trans.GetObject(newDB.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as BlockTable;
                    BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite) as BlockTableRecord;
                    switch (feature)
                    {
                        case "fasadni":
                            LayerTable layerKolekcija = trans.GetObject(newDB.LayerTableId, OpenMode.ForRead) as LayerTable;
                            if (layerKolekcija.Has(feature) == false)
                            {
                                LayerTableRecord ltr = new LayerTableRecord();
                                ltr.Color = Color.FromRgb((byte)random.Next(255), (byte)random.Next(255), (byte)random.Next(255));
                                ltr.Name = feature;
                                layerKolekcija.UpgradeOpen();
                                layerKolekcija.Add(ltr);
                                trans.AddNewlyCreatedDBObject(ltr, true);
                            }
                            idBlocka = bt["fas"];
                            tempTocka = (Point2d)podaciOObjektu.GetValue(1);
                            block = new Autodesk.AutoCAD.DatabaseServices.BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(tempTocka.X, tempTocka.Y, 0), idBlocka);
                            block.Layer = feature; /// <--ERROR HERE eKeyNotFound
                            btr.AppendEntity(block);
                            trans.AddNewlyCreatedDBObject(block, true);
                            break;

 I'm not using ModelessDialog and commandFlag.Session

 

Can someone explain me what is the diference between commandFlags and what should I gain with ModelessDialog form?

 

Thank you all

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.