.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fatal Error while inserting block with DrawJig

1 REPLY 1
Reply
Message 1 of 2
GrzesiekGP
348 Views, 1 Reply

Fatal Error while inserting block with DrawJig

Hi!

 

I want to insert block with showing it when user is moving mouse.


I used this code:

http://adndevblog.typepad.com/autocad/2012/07/how-to-show-a-block-when-positioning-it-using-c.html

 

Everything works fine, but suddendly I'm getting fatal error.

Przechwytywanie.PNG

 

 

The worst thing is that I don't know why.

It happens very rare and the only way to fix this, is copy everything to the new file.

 

In attachments I sent this file.

 

Maybe someone could help me? Or there is some other way to show block while inserting?

 

using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
 
 
namespace MySamples
{
 
  public class BlockJigCmds
  {
 
    // Ask the user to place the block named "TEST", showing it
    //   during the dragging.
    // Make sure that a block with this name exists in the drawing.
    //===============================================================
    [CommandMethod("BlockJig")]
    static public void BlockJig()
    {
      Editor ed =
              Application.DocumentManager.MdiActiveDocument.Editor;
      Database db = ed.Document.Database;
      try
      {
        using (Transaction tr =
                           db.TransactionManager.StartTransaction())
        {
          // Find the "TEST" block in the current drawing
          BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,
                                                   OpenMode.ForRead);
          BlockTableRecord block = (BlockTableRecord)tr.GetObject(
                                       bt["TEST"], OpenMode.ForRead);
 
          // Create the Jig and ask the user to place the block
          //----------------------------------------------------
          MyBlockJig blockJig = new MyBlockJig();
          Point3d point;
          PromptResult res = blockJig.DragMe( block.ObjectId,
                                              out point);
          if (res.Status == PromptStatus.OK)
          {
            // Now we need to do the usual steps to place
            //   the block insert at the position where the user
            //   did the click
            BlockTableRecord curSpace =
              (BlockTableRecord)tr.GetObject(
                               db.CurrentSpaceId, OpenMode.ForWrite);
            BlockReference insert = new BlockReference(point,
                                                     block.ObjectId);
            curSpace.AppendEntity(insert);
            tr.AddNewlyCreatedDBObject(insert, true);
          }
          tr.Commit();
        } // using
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.ToString());
      }
    } // BlockJig()
 
  } // class BlockJigCmds
 
 
  // This Jig will show the given block during the dragging.
  //=================================================================
  public class MyBlockJig : DrawJig
  {
    public Point3d _point;
    private ObjectId _blockId = ObjectId.Null;
 
    // Shows the block until the user clicks a point.
    // The 1st parameter is the Id of the block definition.
    // The 2nd is the clicked point.
    //---------------------------------------------------------------
    public PromptResult DragMe(ObjectId i_blockId, out Point3d o_pnt)
    {
      _blockId = i_blockId;
      Editor ed =
              Application.DocumentManager.MdiActiveDocument.Editor;
 
      PromptResult jigRes = ed.Drag(this);
      o_pnt = _point;
      return jigRes;
    }
 
 
    // Need to override this method.
    // Updating the current position of the block.
    //--------------------------------------------------------------
    protected override SamplerStatus Sampler(JigPrompts prompts)
    {
      JigPromptPointOptions jigOpts = new JigPromptPointOptions();
      jigOpts.UserInputControls =
                          (UserInputControls.Accept3dCoordinates |
                           UserInputControls.NullResponseAccepted);
      jigOpts.Message = "Select a point:";
      PromptPointResult jigRes = prompts.AcquirePoint(jigOpts);
 
      Point3d pt = jigRes.Value;
      if (pt == _point)
        return SamplerStatus.NoChange;
 
      _point = pt;
      if (jigRes.Status == PromptStatus.OK)
        return SamplerStatus.OK;
 
      return SamplerStatus.Cancel;
    }
 
 
    // Need to override this method.
    // We are showing our block in its current position here.
    //--------------------------------------------------------------
    protected override bool WorldDraw(
                  Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
    {
      BlockReference inMemoryBlockInsert =
                               new BlockReference(_point, _blockId);
      draw.Geometry.Draw(inMemoryBlockInsert);
 
      inMemoryBlockInsert.Dispose();
 
      return true;
    } // WorldDraw()
 
  } // class BlockJig
 
} // namespace MySamples

 

Tags (2)
1 REPLY 1
Message 2 of 2
Balaji_Ram
in reply to: GrzesiekGP

Sorry, I am not sure what is causing the crash.

I tried the code with the drawing that you shared and both "TICK_1" and "TICK_3" blocks seem to be getting jigged and inserted correctly.

 

The other way to do this would be to use EntityJig.

In that case a block reference is created even before the jigging startes. On completion the block reference will be added to the modelspace.

 

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

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost