Author Topic: Adding Objects to the DrawingDataBase  (Read 6880 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding Objects to the DrawingDataBase
« Reply #15 on: December 10, 2005, 10:42:51 PM »
Nice Work Kerry

Indeed, very nice.  I really like the idea of breaking the steps down into something easy to follow :)
Can't claim credit for it Bobby, in principle that's straight out of the AutoCAD Docs  .. one of the PowerPoint presentations if I recall.

How was AU for you this year ? Hope you had a good attendance .

Just reading one of your AU2004 Handouts Bobby<CP24-2> .. see that you do  'like the idea of breaking the steps down ..' :-)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Bobby C. Jones

  • Swamp Rat
  • Posts: 514
  • Cry havoc and let loose the dogs of war.
Re: Adding Objects to the DrawingDataBase
« Reply #16 on: December 12, 2005, 10:12:51 PM »
Quote
Just reading one of your AU2004 Handouts Bobby<CP24-2> .. see that you do  'like the idea of breaking the steps down ..' :-)

Ack!  I remember that one :-)  I didn't get good vibes from it.

However, it went *much* better this year.  And it had a lot more code examples.  I'll see if I can get them up on the board this week if anyone is interested.
Bobby C. Jones

Swift

  • Swamp Rat
  • Posts: 596
Re: Adding Objects to the DrawingDataBase
« Reply #17 on: December 13, 2005, 07:21:58 AM »
I'm always interested in a good read!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding Objects to the DrawingDataBase
« Reply #18 on: December 13, 2005, 10:27:44 AM »
Yeah, me too, Great offer Bobby.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding Objects to the DrawingDataBase
« Reply #19 on: January 08, 2006, 07:13:07 PM »
Playing with the next step .. adding multiple Entities to the Database in one Transaction

Any Comments ?

The Tester :
Code: [Select]
using System;
using System.Diagnostics;
using System.Collections;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using AcadEditor = Autodesk.AutoCAD.EditorInput.Editor;
using AcadTransaction = Autodesk.AutoCAD.DatabaseServices.Transaction;
using AcadTransactionManager = Autodesk.AutoCAD.DatabaseServices.TransactionManager;

using KdubTest.Util;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;

// NOTE: LoaderLock exception break toggled OFF to suit vs2005 in AC2006. kwb

[assembly: CommandClass(typeof(KdubTest.TestClass))]
namespace KdubTest
{
    public class TestClass
    {
        //----------------------------
        [CommandMethod("TestCircles", CommandFlags.Modal)]
        static public void TestCircles()
        {
            AcadDatabase db = AcadApplication.DocumentManager.MdiActiveDocument.Database;
           
            // These points are in World UCS, irrespective of current UCS
            // Look at Matrix translations
            Point3d centerPoint_01 = new Point3d(70.0, 70.0, 0.0);
            Vector3d normalZ = new Vector3d(0.0, 0.0, 1.0);
            double radius_01 = 20.0;
            double radius_02 = 30.0;
            double radius_03 = 40.0;
            double radius_04 = 50.0;
            double radius_05 = 60.0;
            double radius_06 = 70.0;           

            Circle circle_01 = new Circle(centerPoint_01, normalZ, radius_01);
            Circle circle_02 = new Circle(centerPoint_01, normalZ, radius_02);
            Circle circle_03 = new Circle(centerPoint_01, normalZ, radius_03);
            Circle circle_04 = new Circle(centerPoint_01, normalZ, radius_04);
            Circle circle_05 = new Circle(centerPoint_01, normalZ, radius_05);
            Circle circle_06 = new Circle(centerPoint_01, normalZ, radius_06);
           
            Tbl.AddEntities(db, new Entity[] {circle_01, circle_02, circle_03, circle_04, circle_05, circle_06 });           
        }
        ////----------------------------
    }
}

From the Library <snipped> :
Code: [Select]
using System;
using System.Diagnostics;
using System.Collections;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using AcadEditor = Autodesk.AutoCAD.EditorInput.Editor;
using AcadTransaction = Autodesk.AutoCAD.DatabaseServices.Transaction;
using AcadTransactionManager = Autodesk.AutoCAD.DatabaseServices.TransactionManager;

namespace KdubTest.Util
{
    public sealed class Tbl
    {
        public Tbl()
        {
        } 
        //-----------------------------
        public static ObjectIdCollection AddEntities(Database db, params Entity[] ents)
        {
            ObjectIdCollection collection = new ObjectIdCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction()) {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); ;
                Entity[] entityArray = ents;
                foreach (Entity ent in ents) {
                    collection.Add(btr.AppendEntity(ent));
                    tr.AddNewlyCreatedDBObject(ent, true);
                }
                tr.Commit();
            }
            return collection;
        }
        //-----------------------------
    }
}
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Adding Objects to the DrawingDataBase
« Reply #20 on: January 08, 2006, 07:48:11 PM »
Kerry,

From my quick look, nicely done. The only thing I would add would be to check the validity of the incoming ents array.
if null or length == 0, return null immediately.

Other than that, which is just personal, it looks solid.

Cheers,
Glenn.
Me

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding Objects to the DrawingDataBase
« Reply #21 on: January 08, 2006, 08:07:44 PM »
Hi Glenn

I have a note on my version to test for null

I did have a test for if ents.length == 0 return null; which worked fine, but still needed to test each array item for null, 'cause it spat the dummy if any of the items were null when I tested it.

I think the code is about as tight as it will ever get.


Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--