Author Topic: SymbolUtilityServices.GetBlockModelSpaceId  (Read 5078 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
SymbolUtilityServices.GetBlockModelSpaceId
« on: December 14, 2007, 11:16:53 PM »
From my Saturday reading ..

public sealed class SymbolUtilityServices
Name: Autodesk.AutoCAD.DatabaseServices.SymbolUtilityServices
Assembly: acdbmgd, Version=17.1.0.0

has a Method
public static ObjectId GetBlockModelSpaceId(Database databasePointer);

In the past , to open Modelspace for Write, I've used something like the following, in one of several variations
Code: [Select]
            ObjectId objId = new ObjectId();

            Database Db = HostApplicationServices.WorkingDatabase;
            AcDb.TransactionManager Tm = Db.TransactionManager;
            using (Transaction tr = Tm.StartTransaction())
            {
                BlockTable tb = (BlockTable)Tm.GetObject
                  (Db.BlockTableId, OpenMode.ForRead, false);
                objId = ((BlockTableRecord)Tm.GetObject
                  (tb[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false)).AppendEntity(ent);
                Tm.AddNewlyCreatedDBObject(entToWriteToDataBase, true);
                tr.Commit();
            }

however, using SymbolUtilityServices.GetBlockModelSpaceId seems a little easier ..

Code: [Select]
            ObjectId objId = new ObjectId();
            Database Db = HostApplicationServices.WorkingDatabase;
            AcDb.TransactionManager Tm = Db.TransactionManager;
            using (Transaction tr = Tm.StartTransaction())
            {
                objId = ((BlockTableRecord)Tm.GetObject(AcDb.SymbolUtilityServices.GetBlockModelSpaceId(Db),
                        OpenMode.ForWrite, false)).AppendEntity(ent);
                Tm.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }

edit for fatFingerSpelling
« Last Edit: December 14, 2007, 11:35:48 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #1 on: December 14, 2007, 11:25:35 PM »
So, an Entity extension Class ( similar to previously posted by Daniel M )

Code: [Select]
// CodeHimBelongaKwb ©  Dec 2007
// D:\Development\Visual Studio 2008\CAD Projects\Sat1215\Sat1215\EntityExtensions.cs
using System;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;

namespace Sat1215
{
    public static partial class EntityExtensions
    {
        internal static ObjectId AddToModelSpace(this Entity ent)
        {
            ObjectId objId = new ObjectId();
            Database Db = HostApplicationServices.WorkingDatabase;
            AcDb.TransactionManager Tm = Db.TransactionManager;
            using (Transaction tr = Tm.StartTransaction())
            {
                objId = ((BlockTableRecord)Tm.GetObject(AcDb.SymbolUtilityServices.GetBlockModelSpaceId(Db),
                        OpenMode.ForWrite, false)).AppendEntity(ent);
                Tm.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return objId;
        }

        internal static ObjectId AddToModelSpace(this Entity ent , Database Db)
        {
            ObjectId objId = new ObjectId();
            AcDb.TransactionManager Tm = Db.TransactionManager;
            using (Transaction tr = Tm.StartTransaction())
            {
                objId = ((BlockTableRecord)Tm.GetObject( AcDb.SymbolUtilityServices.GetBlockModelSpaceId(Db),
                        OpenMode.ForWrite, false)).AppendEntity(ent);
                Tm.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return objId;
        }
    }
}

and to Test it ....

Code: [Select]
// CodeHimBelongaKwb ©  Dec 2007
// D:\Development\Visual Studio 2008\CAD Projects\Sat1215\Sat1215\Commands.cs
using System;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;

 [assembly: CommandClass(typeof(Sat1215.Commands))]

namespace Sat1215
{
    class Commands
    {
        [CommandMethod("test")]
        static public void cmdtest()
        {
            Line ln = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0));
            ln.AddToModelSpace();

            Database Db = HostApplicationServices.WorkingDatabase;
            ln = new Line(new Point3d(100, 100, 0), new Point3d(100, 200, 0));
            ln.AddToModelSpace(Db);
        }
    }
}


The partial classes are irrelevant to the use of the Method, I'm just playing with them here..

« Last Edit: December 14, 2007, 11:43:16 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #2 on: December 14, 2007, 11:29:11 PM »
I'll be getting back into C# down the road, so I'm thanking you, Daniel ... in advance, for surely this stuff will be useful to me.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #3 on: December 14, 2007, 11:31:54 PM »
and a piccy of some of the OTHER Methods and Properties available
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #4 on: December 14, 2007, 11:53:19 PM »
Though, thinking of extension classes ...

This is pretty efficient  AND cool ..

Code: [Select]
       internal static ObjectId ModelSpaceId(this Database Db)
        {
            return SymbolUtilityServices.GetBlockModelSpaceId(Db);
        }

Piccy Added ..


« Last Edit: December 15, 2007, 12:30:55 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #5 on: December 15, 2007, 01:47:00 AM »
This is what I ended up with ..
just need to add XML comments so that the  Intellisense displays the comments.

Code: [Select]
        internal static ObjectId ModelSpaceId(this Database db)
        {
            return SymbolUtilityServices.GetBlockModelSpaceId(db);
        }
        //
        internal static ObjectId AddToModelSpace(this Entity ent)
        {
            return ent.AddToModelSpace(HostApplicationServices.WorkingDatabase);
        }
        //
        internal static ObjectId AddToModelSpace(this Entity ent, Database db)
        {
            ObjectId objId = new ObjectId();
            Acdb.TransactionManager tm = db.TransactionManager;
            using (Transaction tr = tm.StartTransaction())
            {
                objId = ((BlockTableRecord)tm.GetObject(db.ModelSpaceId(),
                        OpenMode.ForWrite, false)).AppendEntity(ent);
                tm.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return objId;
        }
« Last Edit: December 15, 2007, 02:55:05 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8718
  • AKA Daniel
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #6 on: December 15, 2007, 01:53:05 AM »
Great stuff Kerry! FYI in the 2006 API the GetBlockModelSpaceId() & GetBlockPaperSpaceId()
methods are not static, so people using this API will need to create a new instance of the class.
It might be wise to use the other method to get the Space id for compatibility.


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #7 on: December 15, 2007, 02:11:04 AM »

Thanks for the heads-up about the 2006 API.
.. the old backward compatibility issue ... yuk !

I'm developing for AC2007, 2008, 2009 so I think I'll ignore 2006 issues.
 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #8 on: December 15, 2007, 02:12:57 AM »
This is the Paperspace stuff using SymbolUtilityServices

Code: [Select]
        /// <summary>
        /// Gets and return the PaperSpace ObjectId.
        /// </summary>
        /// <param name="db">Database to use</param>
        /// <returns>The PaperSpace ObjectId of the Database parameter</returns>
        internal static ObjectId PaperSpaceId(this Database db)
        {
            return SymbolUtilityServices.GetBlockPaperSpaceId(db);
        }
        //
        internal static ObjectId AddToPaperSpace(this Entity ent)
        {           
            return ent.AddToPaperSpace(HostApplicationServices.WorkingDatabase);
        }
        //
        internal static ObjectId AddToPaperSpace(this Entity ent, Database db)
        {
            ObjectId objId;
            Acdb.TransactionManager tm = db.TransactionManager;
            using (Transaction tr = tm.StartTransaction())
            {
                objId = ((BlockTableRecord)tm.GetObject(db.PaperSpaceId(),
                        OpenMode.ForWrite, false)).AppendEntity(ent);
                tm.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return objId;
        }
« Last Edit: December 15, 2007, 02:53:09 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #9 on: December 15, 2007, 02:15:59 AM »
.. and this is the associated Intellisence comments from the compiled XML comment. ( for anyone who hasn't made use of the feature :-) )
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #10 on: December 15, 2007, 02:50:36 AM »

The Database object already has a CurrentSpaceId getter, so when Writing to the Current Space we don't need to concoct our own. 

Code: [Select]
        internal static ObjectId AddToCurrentSpace(this Entity ent)
        {
            return ent.AddToCurrentSpace(HostApplicationServices.WorkingDatabase);
        }
        //
        internal static ObjectId AddToCurrentSpace(this Entity ent, Database db)
        {
            ObjectId objId;
            Acdb.TransactionManager tm = db.TransactionManager;
            using (Transaction tr = tm.StartTransaction())
            {
                objId = ((BlockTableRecord) tm.GetObject(db.CurrentSpaceId,
                        OpenMode.ForWrite, false)).AppendEntity(ent);
                tm.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return objId;
        }

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

Glenn R

  • Guest
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #11 on: December 15, 2007, 11:32:04 AM »
public sealed class SymbolUtilityServices

How did I miss this class!?! <shakes head> Nice find Kerry...you just keep on reading ;)

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SymbolUtilityServices.GetBlockModelSpaceId
« Reply #12 on: December 15, 2007, 06:16:29 PM »
>>>>. ..you just keep on reading ;)

Cheers,
Glenn.

I certainly will  :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.