Author Topic: Jig Insert Block  (Read 3443 times)

0 Members and 1 Guest are viewing this topic.

cadpro

  • Guest
Jig Insert Block
« on: February 08, 2009, 01:31:07 AM »
Hello,

In C#, can anybody give me some example code to insert a drawing as block using jig?

Thanks

cadpro

  • Guest
Re: Jig Insert Block
« Reply #1 on: February 10, 2009, 02:30:53 AM »
 I found some code and modified it according to my requirement. But this code sends 'Insertion Point' twice. Could anybody point where I have gone wrong?

           
Code: [Select]
[CommandMethod("test")]
            public void test()
            {
                string fName=@"C:\a.dwg";

                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                //Get the editor object
                Editor ed = doc.Editor;

                string blkName="a";

                //Get the working database
                Database dwg = doc.Database;

                //Start a transaction
                Transaction tr = doc.TransactionManager.StartTransaction();

                try
                {
                    //Create temporary database to read in block
                    Database dbDwg = new Database();

                    //Read the file from disk
                    dbDwg.ReadDwgFile(fName, System.IO.FileShare.Read, true, "");

                    //Insert it into the current database.
                    dwg.Insert(blkName, dbDwg, false);

                    dbDwg.Dispose();

                     //Open the block for read.
                    BlockTable bt = (BlockTable)tr.GetObject(dwg.BlockTableId, OpenMode.ForRead);

                    //See if the block table record exists
                    if (bt.Has(blkName))
                    {
                        //Prompt user for insertion point
                        PromptPointOptions po = new PromptPointOptions("Select insertion point: ");

                        //Get user to select the insertion point
                        PromptPointResult pr = ed.GetPoint(po);

                        //user selected a valid point
                        if (pr.Status == PromptStatus.OK)
                        {
                            //fix up the point to be a Point3D object
                            Point3d pt = new Point3d(pr.Value.X, pr.Value.Y, pr.Value.Z);

                            ObjectId bId = bt[blkName];

                                //insert the block at the insertion point
                                BlockReference br = new BlockReference(pt, bId);

                                BlockJig blkJig = new BlockJig(br);

                                //Perform the jig operation
                                pr = ed.Drag(blkJig);

                                if (pr.Status == PromptStatus.OK)
                                {
                                    //Add block reference to the current space
                                    BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);

                                    //Add the block reference to the current space
                                     curSpace.AppendEntity(blkJig.GetEntity());

                                    //Tell the transaction
                                     tr.AddNewlyCreatedDBObject(blkJig.GetEntity(), true);

                                    //Commit transaction to database
                                    tr.Commit();
                                }
                        }
                        else
                        {
                            MessageBox.Show("Block " + blkName + " does not exists");
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    //Always dispose of transaction
                    tr.Dispose();
                }   

            }

Thanks

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Jig Insert Block
« Reply #2 on: February 10, 2009, 03:41:13 AM »

Have you tried stepping through the code in debug mode ?
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.

cadpro

  • Guest
Re: Jig Insert Block
« Reply #3 on: February 10, 2009, 06:45:18 AM »
Yes, I did and got to know that the 'Select Insertion Point' in the code first, then goes to the BlockJig Class and throws another command line 'Enter insert point' agian. If I remove the prompt result in the code, how do I loop through the prompt result until the user cancels the command?

Thanks

sinc

  • Guest
Re: Jig Insert Block
« Reply #4 on: February 10, 2009, 08:15:32 AM »
Your code is doing exactly what you are telling it to do.  You have a call to ed.GetPoint, which is your first prompt, and then a call to ed.DragJig, which is your second prompt.

I take it this is not your complete code...?  Did you write a BlockJig class?

Have you looked at this?

http://through-the-interface.typepad.com/through_the_interface/2007/05/using_a_jig_fro.html

cadpro

  • Guest
Re: Jig Insert Block
« Reply #5 on: February 11, 2009, 12:59:16 AM »
Yes, I did get the BlockJig code from Kean's website.

In fact, the code is working, but I'm looking to multiply insert blocks which I cannot get to work.

Thanks
« Last Edit: February 11, 2009, 01:13:05 AM by cadpro »

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Jig Insert Block
« Reply #6 on: February 11, 2009, 09:21:16 AM »
// We loop until the jig is cancelled

            while (pr.Status == PromptStatus.OK)

            {

sinc

  • Guest
Re: Jig Insert Block
« Reply #7 on: February 11, 2009, 04:03:52 PM »
So did you get this figured out yet?

If not, did you actually run Kean's code, the way he has it written in his blog?  If not, you should try single-stepping through it, until you figure out how it works.  [He actually does something that I don't understand why he does, with that call to entJig.GetEntity() toward the end...  That seems unnecessary to me.  After all, the call to entJig.GetEntity() merely returns his BlockReference "br", which he already has...  So he could simply add "br" to the transaction.  But that's a side point...]

The important thing to look at is how the block gets created and positioned in Kean's code.  Notice that he does NOT prompt the user to pick a selection point, the way you are doing.  The block gets positioned in the BlockJig.Update() method, which gets called during the ed.Drag() operation.