Author Topic: Looking for some example code to insert block. (C#)  (Read 12993 times)

0 Members and 1 Guest are viewing this topic.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Looking for some example code to insert block. (C#)
« Reply #15 on: November 02, 2005, 01:28:37 PM »

This is not original .. I don't know the author


I recognize that code.  I wouldn't trust the author as far as you could throw him  :-P
Bobby C. Jones

TR

  • Guest
Re: Looking for some example code to insert block. (C#)
« Reply #16 on: November 02, 2005, 01:38:50 PM »
Thanks for (indirectly) supplying the code Bobby.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Looking for some example code to insert block. (C#)
« Reply #17 on: November 02, 2005, 01:39:25 PM »
Why it's Mr. Jones.

Howdy Bobby.

- Michael.
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: Looking for some example code to insert block. (C#)
« Reply #18 on: November 02, 2005, 07:03:32 PM »

This is not original .. I don't know the author


I recognize that code.  I wouldn't trust the author as far as you could throw him  :-P

hehehehehe ..
now I know :) .. good to hear <the first part anyway>


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.

Chuck Gabriel

  • Guest
Re: Looking for some example code to insert block. (C#)
« Reply #19 on: November 02, 2005, 07:35:30 PM »
Hola Bobby.  Long time no see.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Looking for some example code to insert block. (C#)
« Reply #20 on: November 03, 2005, 10:36:20 AM »
Hola Bobby.  Long time no see.

Hey Chuck!  They let anybody into this place don't they CG, LE, MP...  :-P

It's good to here from you.
Bobby C. Jones

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looking for some example code to insert block. (C#)
« Reply #21 on: September 16, 2009, 11:07:01 AM »
Just out of curiosity, is the above code still good in 2010?  I am having a very hard time getting it to read the external file.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looking for some example code to insert block. (C#)
« Reply #22 on: September 16, 2009, 03:07:09 PM »
got it working (Thanks Tim).  Moving ahead...
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looking for some example code to insert block. (C#)
« Reply #23 on: September 21, 2009, 05:08:14 PM »
ok, I am having a problem, which is probably something simple I am overlooking.
Code: [Select]
[CommandMethod("TEPWireLabel")]
        public static void WireLabel()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptKeywordOptions pko = new PromptKeywordOptions("");
            pko.Message = "\nJustification and Quanity";
            pko.Keywords.Add("L1");
            pko.Keywords.Add("L2");
            pko.Keywords.Add("L3");
            pko.Keywords.Add("L4");
            pko.Keywords.Add("L5");
            pko.Keywords.Add("L6");
            pko.Keywords.Add("R1");
            pko.Keywords.Add("R2");
            pko.Keywords.Add("R3");
            pko.Keywords.Add("R4");
            pko.Keywords.Add("R5");
            pko.Keywords.Add("R6");
            pko.Keywords.Default = "L3";
            pko.AllowNone = false;

            PromptResult pkr = doc.Editor.GetKeywords(pko);
            int intOsnapMode = System.Convert.ToInt32(Application.GetSystemVariable("OSMODE"));
            Application.SetSystemVariable("OSMODE", 1);

            PromptPointOptions ppo = new PromptPointOptions("\nSelect Point: ");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nPoint not selected, Error!");
            }

            string strPath = @"\\autocad\AcadCustomizations\SYMBOLS\";
            string strBlock = pkr.StringResult + "DESC";


            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord CurrentSpaceBTR = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                if (bt.Has(strBlock))
                {
                    ObjectId tmpBlockId = bt[strBlock];
                    BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
                    BlockReference br = new BlockReference(ppr.Value, tmpBlockId);
                    CurrentSpaceBTR.AppendEntity(br);
                    if (blkDefRecord.HasAttributeDefinitions)
                    {
                        foreach (ObjectId idAtt in blkDefRecord)
                        {
                            AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
                            if (attDef != null)
                            {
                                AttributeReference attRef = new AttributeReference();
                                attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                                br.AttributeCollection.AppendAttribute(attRef);
                                tr.AddNewlyCreatedDBObject(attRef, true);
                            }
                        }
                        tr.AddNewlyCreatedDBObject(br, true);
                    }
                }
                else
                {
                    string sourceFileName = @"\\autocad\AcadCustomizations\SYMBOLS\L3DESC.dwg";
                    try
                    {
                        using ( Database tempDb = new Database( false, true ))
                        {
                            tempDb.ReadDwgFile(sourceFileName, FileShare.Read, true, null);
                            db.Insert(strBlock, tempDb, false);
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception e)
                    {
                        ed.WriteMessage(e.Message);
                    }


                }
                tr.Commit();

            }
            Application.SetSystemVariable("OSMODE", intOsnapMode);
        }
If I manually insert the block first, everything works as expected.

If I Do Not insert the block first, it goes and gets the block, but doesn't insert it. (it might be there, just not visable)  It does define the block in the BT though, because I can manually insert it after running through the code.

On a side note, I thought I saw a thread about direct cast VS. using AS,  any preference or situations where one works and the other doesn't?
« Last Edit: September 21, 2009, 05:24:47 PM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looking for some example code to insert block. (C#)
« Reply #24 on: September 21, 2009, 05:20:02 PM »
Nevermind on the code question above, I'm an idiot with serious PEBCAK issuses.  I guess I should finish writing the code before I test it.

I am still interested in the () VS AS question
« Last Edit: September 21, 2009, 05:25:46 PM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

sinc

  • Guest
Re: Looking for some example code to insert block. (C#)
« Reply #25 on: September 21, 2009, 06:21:18 PM »
A direct cast will throw an exception on failure.  "as" will set the value to null on failure.

Code: [Select]
T value1 = (T)value2;
The above will throw an exception if value2 cannot be cast to type T.

Code: [Select]
T value1 = value2 as T;
The above will set value1 equal to NULL if value2 cannot be cast to type T.


David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looking for some example code to insert block. (C#)
« Reply #26 on: September 21, 2009, 06:26:22 PM »
Thanks Sinc, that makes perfect sense. (Not bad for a monday afternoon)  So which is better? (I know that is a loaded question).  Seems to me setting to null is better than an exception, but would require test for null every time, where as I would only have to deal with the exception when it was thrown.  Having typed all that, I think I might prefer the exception now.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Looking for some example code to insert block. (C#)
« Reply #27 on: September 21, 2009, 10:18:40 PM »
personally I'd rather use the 'AS' keyword
and test for null  locally because we are really doing data assertion, which may/could have specific requirements and subsequent actions ...
then use exception catching for 'exceptional' situations.

... perhaps I'm a little anal though :)

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.

sinc

  • Guest
Re: Looking for some example code to insert block. (C#)
« Reply #28 on: September 22, 2009, 04:39:31 PM »
One isn't inherently "better" than the other.  It depends on what the rest of your code is doing.

Usually, exception handling involves some extra overhead, so I usually try to avoid code that throws exceptions as a "normal" part of the operation.  So if all other things are equal, I would tend to prefer checking for a null, instead of catching an exception.

But depending on the rest of your code, and the conditions you expect during runtime, you may prefer one or the other in different situations.

For example, when we get objects from a transaction, we get DBObjects that we then generally must cast to a more-specific type.  If we are getting an object that the user selected on-screen, then the object may be one of a variety.  We can use various methods to check for the type of the selected object, but the simplest is often to use "as".  For example:

Code: [Select]
               PromptEntityResult ssResult = ed.GetEntity(promptOptions);
                if (ssResult.Status == PromptStatus.OK)
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        DBObject dbObj = tr.GetObject(ssResult.ObjectId, OpenMode.ForRead, false);
                        Line line = dbObj as Line;
                        if (line != null)
                        {
                            // work with the line
                        }
                        else
                        {
                            Circle circle = dbObj as Circle;
                            if (circle != null)
                            {
                                // work with the circle
                            }
                            // repeat as necessary for more types
                        }

On the other hand, sometimes an object should pretty much always be of the expected type, and if the cast fails, it would indicate some sort of extraordinary circumstance.  In this situation, it might make more sense to use a direct cast, which would result in an exception on failure.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: Looking for some example code to insert block. (C#)
« Reply #29 on: September 22, 2009, 06:10:00 PM »
Use a cast when you think you 'want' an exception if the object is of the wrong type.
Use a cast when un-boxing value types as 'as' only works with reference types.