.NET

Reply
Contributor
$o-olitarY
Posts: 16
Registered: ‎10-20-2013
Message 1 of 3 (288 Views)
Accepted Solution

How to using Modeless form in AutoCAD

288 Views, 2 Replies
11-26-2013 02:53 AM

Hello, 

I have a problem.

I want to AutoCAD do not lock it's space, when my Form is shown on display.

But I have an exception when I use modeless dialog.

 

public void MyCommand ()
{
  //-------------------------------------------------

  // Using this code, my program works fine, but locks modal space of AutoCAD

  System.Windows.Forms.Application.EnableVisualStyles ();
  System.Windows.Forms.Application.Run (new  Form1 ());

 

  // I want to use this instruction to do not lock modal space
  // Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog (new Form1 ());
  //------------------------------------------------
}

 

If I use modeless dialog I can read and write from acad prompt and create block; BUT I can't create BlockReference. 

I get an exception "eLockViolation" in the next code instructions:

 

//------------------------------------------------------------------------------------------------------------------------------

public void CreateBlockReference (string strBlockName, Point3d Origin)
{
  using( Transaction t = db.TransactionManager.StartTransaction () )
  {
   BlockTable btTable = (BlockTable)t.GetObject (db.BlockTableId, OpenMode.ForRead);  // <--- EXCEPTION

   BlockTableRecord btrModelSpace = (BlockTableRecord)t.GetObject (
     btTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

   if( !btTable.Has (strBlockName) )
   {
     ed.WriteMessage (string.Format (msgs.BlockNoExist,strBlockName));
     throw new Exception (ErrorStatus.MissingBlockName, 
     string.Format (msgs.BlockNoExist,strBlockName));
   }
   ObjectId myBlockId = btTable[strBlockName];

   BlockReference brRefBlock = new BlockReference (Origin, myBlockId);

   btrModelSpace.AppendEntity (brRefBlock);
   t.AddNewlyCreatedDBObject (brRefBlock, true);

   t.Commit ();
  } // end using
}

//------------------------------------------------------------------------------------------------------------------------------

 

Please, help me.

Thank you.

I found right answer:

 

/// <summary></summary>
/// <param name="strBlockName"></param>
/// <param name="Origin"></param>
public void CreateBlockReference (string strBlockName, Point3d Origin)
{
  DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument ();

  using( Transaction t = db.TransactionManager.StartTransaction () )
 {
  BlockTable btTable = (BlockTable)t.GetObject (db.BlockTableId, OpenMode.ForRead);

  BlockTableRecord btrModelSpace = (BlockTableRecord)t.GetObject (
    btTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

  if( !btTable.Has (strBlockName) )
  {
    ed.WriteMessage (string.Format (msgs.BlockNoExist,strBlockName));
    throw new Exception (ErrorStatus.MissingBlockName, 
    string.Format (msgs.BlockNoExist,strBlockName));
  }
  ObjectId myBlockId = btTable[strBlockName];

 

  BlockReference brRefBlock = new BlockReference (Origin, myBlockId);
 
  btrModelSpace.AppendEntity (brRefBlock);
  t.AddNewlyCreatedDBObject (brRefBlock, true);

  t.Commit ();
  } // end using
  dl.Dispose ();
}

Contributor
$o-olitarY
Posts: 16
Registered: ‎10-20-2013
Message 2 of 3 (283 Views)

Re: How to using Modeless form in AutoCAD

11-26-2013 03:19 AM in reply to: $o-olitarY

I found right answer:

 

/// <summary></summary>
/// <param name="strBlockName"></param>
/// <param name="Origin"></param>
public void CreateBlockReference (string strBlockName, Point3d Origin)
{
  DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument ();

  using( Transaction t = db.TransactionManager.StartTransaction () )
 {
  BlockTable btTable = (BlockTable)t.GetObject (db.BlockTableId, OpenMode.ForRead);

  BlockTableRecord btrModelSpace = (BlockTableRecord)t.GetObject (
    btTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

  if( !btTable.Has (strBlockName) )
  {
    ed.WriteMessage (string.Format (msgs.BlockNoExist,strBlockName));
    throw new Exception (ErrorStatus.MissingBlockName, 
    string.Format (msgs.BlockNoExist,strBlockName));
  }
  ObjectId myBlockId = btTable[strBlockName];

 

  BlockReference brRefBlock = new BlockReference (Origin, myBlockId);
 
  btrModelSpace.AppendEntity (brRefBlock);
  t.AddNewlyCreatedDBObject (brRefBlock, true);

  t.Commit ();
  } // end using
  dl.Dispose ();
}

Board Manager
Daniel.Du
Posts: 225
Registered: ‎12-23-2008
Message 3 of 3 (225 Views)

Re: How to using Modeless form in AutoCAD

12-03-2013 12:46 AM in reply to: $o-olitarY

yes, use DocumentLock in a model or modeless dialouge.



Daniel Du
Developer Technical Services
Autodesk Developer Network

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!