.NET

Reply
Member
marcwalen
Posts: 4
Registered: ‎04-26-2013
Message 1 of 3 (380 Views)
Accepted Solution

Creating a block

380 Views, 2 Replies
04-26-2013 03:58 AM

Hi all,

 

I'm new to .net for autocad, but very familiar with c#. I studied some samples about creating a block from c#, but need some help figuring it out.

 

Waht I'm trying to do:

I have a form for some basic input. When the user presses OK the code checks if the blockname is aleady in use. When no it creates the block using following code:

 

private void make_block()
        {
            double nextpoint = 0.0;
            double linedis = 7.3634;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = new BlockTableRecord();
                btr.Name = "hoeveelheden";

                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);

                btr.Origin = Point3d.Origin;
                btr.Units = UnitsValue.Meters;

                ObjectId btrId = bt.Add(btr);
                bt.DowngradeOpen();

                DBObjectCollection ents = new DBObjectCollection();

                //controleren op (de)markeren
                double tmp = conlengte + lengte0505 + lengte11 + lengte13 + lengte31 + lengte39 + lengte30270 + lengte93 + haailengte5050 + haailengte5070 + demconlengte +
                    demlengte0505 + demlengte11 + demlengte13 + demlengte31 + demlengte39 + demlengte30270 + demlengte93;
                if (tmp != 0.0 || pijlen != 0)
                {
                    //(de)markeren toevoegen aan block
                    Point3d p1 = new Point3d(0.0, nextpoint, 0.0);
                    Point3d p2 = new Point3d(0.0, nextpoint + 100.0, 0.0);
                    Line ln = new Line(p1, p2);
                    ln.Layer = "wx-zwart-004";
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 100.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 100.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 90.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 90.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);

                    p1 = new Point3d(7.0, nextpoint + 92.5, 0.0);
                    DBText acText = new DBText();
                    acText.SetDatabaseDefaults();
                    acText.Position = p1;
                    acText.Height = 5.0;
                    acText.TextString = "markering";
                    acText.Layer = "wx-zwart-004";
                    ents.Add(acText);

					//some more text entities are created

                    nextpoint = nextpoint + 100.0;
                }

                //controleren op barrier
                tmp = barrier + barrier_beton + barrier_Bever + barrier_HaiSafe + barrier_Haitsma_Step + barrier_kunststof +
                    barrier_Miniguard + barrier_Romein + barrier_RWS + barrier_Safe_guard + barrier_staal + barrier_Vario_guard;
                if (tmp != 0.0 || pstuk != 0 || rimobP110s != 0 || rimobV270 != 0 || antizichtscherm != 0.0)
                {
                    //(de)markeren toevoegen aan block
                    Point3d p1 = new Point3d(0.0, nextpoint, 0.0);
                    Point3d p2 = new Point3d(0.0, nextpoint + 115.0, 0.0);
                    Line ln = new Line(p1, p2);
                    ln.Layer = "wx-zwart-004";
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 115.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 115.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 105.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 105.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);

                    p1 = new Point3d(7.0, nextpoint + 107.5, 0.0);
                    DBText acText = new DBText();
                    acText.SetDatabaseDefaults();
                    acText.Position = p1;
                    acText.Height = 5.0;
                    acText.TextString = "geleidebarrier";
                    acText.Layer = "wx-zwart-004";
                    ents.Add(acText);

                    if (barrier_staal != 0.0)
                    {
                        p1 = new Point3d(52.0, nextpoint + (93.5 - 1 * linedis), 0.0);
                        acText = new DBText();
                        acText.SetDatabaseDefaults();
                        acText.Position = p1;
                        acText.Height = 3.5;
                        acText.TextString = Math.Round(barrier_staal, 0).ToString() + " m";
                        acText.Layer = "wx-zwart-002";
                        ents.Add(acText);
                    }

					//some more text entities are created

                    nextpoint = nextpoint + 115.0;
                }

                //controleren op geleidebakens / kegels
                if (baken != 0 || kegel != 0)
                {
                    //(geleidebakens/kegels toevoegen aan block
                    Point3d p1 = new Point3d(0.0, nextpoint, 0.0);
                    Point3d p2 = new Point3d(0.0, nextpoint + 100.0, 0.0);
                    Line ln = new Line(p1, p2);
                    ln.Layer = "wx-zwart-004";
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 40.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 40.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 30.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 30.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);

                    p1 = new Point3d(7.0, nextpoint + 32.5, 0.0);
                    DBText acText = new DBText();
                    acText.SetDatabaseDefaults();
                    acText.Position = p1;
                    acText.Height = 5.0;
                    acText.TextString = "geleidebakens / kegels";
                    acText.Layer = "wx-zwart-004";
                    ents.Add(acText);

					//some more text entities are created

                    nextpoint = nextpoint + 40.0;
                }

                //controleren op verlichting
                if (lichtmast != 0 || mobilight != 0)
                {
                    //(verlichting toevoegen aan block
                    Point3d p1 = new Point3d(0.0, nextpoint, 0.0);
                    Point3d p2 = new Point3d(0.0, nextpoint + 100.0, 0.0);
                    Line ln = new Line(p1, p2);
                    ln.Layer = "wx-zwart-004";
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 40.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 40.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);
                    p1 = new Point3d(0.0, nextpoint + 30.0, 0.0);
                    p2 = new Point3d(150.0, nextpoint + 30.0, 0.0);
                    ln = new Line(p1, p2);
                    ents.Add(ln);

                    p1 = new Point3d(7.0, nextpoint + 32.5, 0.0);
                    DBText acText = new DBText();
                    acText.SetDatabaseDefaults();
                    acText.Position = p1;
                    acText.Height = 5.0;
                    acText.TextString = "tijdelijke verlichting";
                    acText.Layer = "wx-zwart-004";
                    ents.Add(acText);

		    //some more text entities are created

                }

                foreach (Entity ent in ents)
                {
                    btr.AppendEntity(ent);
                    tr.AddNewlyCreatedDBObject(ent, true);
                }

                tr.AddNewlyCreatedDBObject(btr, true);                

                tr.Commit();
            }
        }

 

The code runs succesfully (meaning i don't get any errors), but when I want to insert the block it isn't in the list of blocks. I must be forgetting something, but am unable to figure out what.

Only skimming your code, I see nothing suspicious.

 

If there are other transactions being started that you don't show in your code, make sure all of them are comitted.

 

If your code is being called in one or more try{} blocks, make sure the catch{} blocks are not hiding an error that might also cause a transaction to abort.

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 3 (348 Views)

Re: Creating a block

04-26-2013 01:03 PM in reply to: marcwalen

Only skimming your code, I see nothing suspicious.

 

If there are other transactions being started that you don't show in your code, make sure all of them are comitted.

 

If your code is being called in one or more try{} blocks, make sure the catch{} blocks are not hiding an error that might also cause a transaction to abort.

 

Member
marcwalen
Posts: 4
Registered: ‎04-26-2013
Message 3 of 3 (318 Views)

Re: Creating a block

04-27-2013 12:48 PM in reply to: DiningPhilosopher

I went trough my code again, indeed found a transaction which I didn't dispose. Now it works just perfect. Thanks for the tip!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.