Author Topic: Autocad PromptResult question  (Read 3557 times)

0 Members and 1 Guest are viewing this topic.

GRH

  • Guest
Autocad PromptResult question
« on: April 03, 2016, 07:00:31 AM »
Hi,

Could someone please help me understand the PromtResult class in autocad API...

Im writing some code that creates a block from selected entites, then drags (jig) the block to where ever.

My current code asks the user for a block name to insert, this is fine for most part, but I also want to just parse the block id directly without user input...

How can I use PromptResult = PromptStatus.ok  until Right Click or ESC button is used, obviously to stop some code executing if the user wants to exit?

GRH


Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Autocad PromptResult question
« Reply #1 on: April 03, 2016, 09:23:47 AM »
Hi,

Something like this?
NOTE:Change the Block name "S" to your desired one.

Code - C#: [Select]
  1.             using (Transaction tr = db.TransactionManager.StartTransaction())
  2.             {
  3.                 try
  4.                 {
  5.                     BlockTable BlkTbl = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  6.                     var spc = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  7.                     var op = new PromptPointOptions("\nSpecify point to insert the block :");
  8.                     op.AllowNone = true;
  9.                     while (true)
  10.                     {
  11.                         PromptPointResult pt = ed.GetPoint(op);
  12.                         if (pt.Status == PromptStatus.OK)
  13.                         {
  14.                             BlockTableRecord BlkRec = (BlockTableRecord)BlkTbl["S"].GetObject(OpenMode.ForWrite);
  15.                             BlockReference MyBlock = new BlockReference(pt.Value, BlkRec.ObjectId);
  16.                             MyBlock.Layer = "0";
  17.                             spc.AppendEntity(MyBlock);
  18.                             tr.AddNewlyCreatedDBObject(MyBlock, true);
  19.                             db.TransactionManager.QueueForGraphicsFlush();
  20.                         }
  21.                         else break;
  22.                     }
  23.                 }
  24.                 catch (System.Exception x)
  25.                 {
  26.                     ed.WriteMessage("\nError: " + x.Message);
  27.                 }
  28.                 tr.Commit();
  29.             }

GRH

  • Guest
Re: Autocad PromptResult question
« Reply #2 on: April 05, 2016, 12:09:13 AM »
Hi and thanks for reply,

im a bit confused with your code as the user input for the insert point on mine is part of my jig class (Overrides Function Sampler)... see below

Code - Visual Basic: [Select]
  1.         Public Function BlockWithJig(ByVal BlockName As String, ByVal Point As Point3d) As ObjectId
  2.             Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  3.             Dim db As Database = doc.Database
  4.             Dim ed As Editor = doc.Editor
  5.             Dim pr As PromptResult = Nothing
  6.  
  7.             ' exit function if detect ESC or RightClick
  8.  
  9.             Using tr As Transaction = doc.TransactionManager.StartTransaction()
  10.                 Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
  11.                 If Not bt.Has(BlockName) Then
  12.  
  13.                 Else
  14.                     Dim bdId As ObjectId = bt(BlockName)
  15.                     Dim br As New BlockReference(Point, bdId)
  16.                     Dim entJig As New BlockJig(br)
  17.                     pr = ed.Drag(entJig) 'perform the jig
  18.                    Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
  19.                     ms.AppendEntity(entJig.GetEntity())
  20.                     tr.AddNewlyCreatedDBObject(entJig.GetEntity(), True)
  21.                     doc.TransactionManager.QueueForGraphicsFlush()
  22.                     tr.Commit()
  23.                     Return bdId
  24.                 End If
  25.             End Using
  26.         End Function

GRH

« Last Edit: April 05, 2016, 02:15:05 AM by GRH »

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Autocad PromptResult question
« Reply #3 on: April 05, 2016, 01:20:46 AM »
Hey GRH, if you insert more than one blockref, it might not make sense to return an ObjectID. Maybe an ObjectIdCollection would be more appropriate. You can stick the Prompt Result in a do while loop to add more. It might look something like this:

Code - C#: [Select]
  1. public ObjectIdCollection Jiggety()
  2. {
  3.   ObjectIdCollection retCollection = new ObjectIdCollection();
  4.   PromptResult blResult;
  5.  
  6.   do
  7.   {
  8.     BlockJig blJig = new BlockJig();
  9.     // Jig it
  10.     blResult = Active.Editor.Drag(blJig);
  11.     if (blResult.Status == PromptStatus.Keyword)
  12.     {
  13.       switch (blResult.StringResult)
  14.       {
  15.         case "Q":
  16.           // do keyword stuff
  17.           break;
  18.         case "A":
  19.           // do keyword stuff
  20.           break;
  21.       }
  22.     }
  23.     else if (blResult.Status == PromptStatus.OK)
  24.     {
  25.       // add block to current space
  26.       using (LockedTransaction tr = Active.Document.TransactionManager.StartLockedTransaction())
  27.       {
  28.         BlockTableRecord blockDef = tr.GetObject(blockId, OpenMode.ForRead) as BlockTableRecord;
  29.         BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(Active.Database.CurrentSpaceId, OpenMode.ForWrite);
  30.         BlockReference br = new BlockReference(blJig.GetEntity().Position, blockId);
  31.  
  32.         // set properties and add to currentSpace
  33.         br.Rotation = rotation;
  34.         br.ScaleFactors = scale;
  35.         ObjectId returnId = curSpace.AppendEntity(br);
  36.         retCollection.Add(returnId);
  37.         tr.AddNewlyCreatedDBObject(br, true);
  38.         tr.Commit();
  39.       }
  40.     else //if(blResult.Status!=PromptStatus.OK||blResult.Status!=PromptStatus.Keyword)
  41.     {
  42.       canceled = true;
  43.     }
  44.   } while (blResult.Status != PromptStatus.Cancel &&
  45.            blResult.Status != PromptStatus.Error && !canceled);
  46.   return retCollection;
  47. }
  48.  
  49.  
« Last Edit: April 05, 2016, 01:24:49 AM by Atook »

GRH

  • Guest
Re: Autocad PromptResult question
« Reply #4 on: April 05, 2016, 02:08:37 AM »
Thanks Mate,

sorry for my ignorance, but I only code in VB.net, I tried to convert this online but I got errors.....

Any chance you could update your post to show in VB.net ?

cheers

GRH

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8696
  • AKA Daniel
Re: Autocad PromptResult question
« Reply #5 on: April 05, 2016, 02:22:39 AM »
Any chance you could update your post to show in VB.net ?

ouch  :|


Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Autocad PromptResult question
« Reply #6 on: April 05, 2016, 02:39:04 AM »
Any chance you could update your post to show in VB.net ?
Sorry man, the code's not meant to compile, I just put it together for illustration sake. I'd probably butcher the VB version worse than the C# I posted.

If you're having trouble reading the C#, here are the take aways:

  • Drag your jig inside a do/while loop, using the prompt.Status as a check whether or not to exit the loop
  • If you're inserting more than one blockRef, you probably should return multiple ObjectIDs (if you're using them) consider returning an ObjectIdCollection

Other aspects of the code (current space vs modelspace, etc) I just left in because I use them in my routines, and thought they might be helpful.

GRH

  • Guest
Re: Autocad PromptResult question
« Reply #7 on: April 05, 2016, 05:29:08 AM »
Thanks,

this only ever inserts one blockRef at a time....

I have a plan now, thanks..

GRH