I was wondering if there is a way to programatically force a block to open in the block editor? I haven't had much success with my trials thus far.
I am looping through the blocks in the drawing, I want to open them in the block editor, take a screenshot, and close.
Unfortunately I am stuck at step two currently, any help would be greatly appreciated!
I have tried that previously and when you put the space in it treats the space as an enter button key press. I went ahead and tried it again just to be sure with the following code. Instead of trying to open the bedit on each block I tried having it run on just the last one that it finds. What occurs is my command executes and when it finishes it tries to execute the -bedit command which ends up prompting me to provide a block name. I tried spoofing another "enter" keypress by putting a space after the block name which also did not work. This also crashes AutoCAD for some reason...
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
string blkname = "";
foreach (ObjectId btrid in bt)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrid, OpenMode.ForRead);
foreach (ObjectId id in btr)
{
try
{
BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForRead, false);
ed.WriteMessage("\nBlock Name: \"{0}\" Object ID: \"{1}\"", br.Name, br.ObjectId);
blkname = br.Name;
//doc.SendStringToExecute("-bedit ", true, false, false);
//doc.SendStringToExecute(br.Name + " ", true, false, false);
//doc.SendStringToExecute("bclose ", true, false, false);
}
catch { }
}
}
doc.SendStringToExecute("-bedit " + blkname,true, false, false);
First, stop the crash, wrap ALL of it in a (vb.net)
Try
'all your code
Catch ex as SYSTEM.Exception
MsgBox(ex.tostring)
End Try
I don't get this line:
ed.WriteMessage(""& vbLf&"Block Name: \""{0}\"" Object ID: \""{1}\""", br.Name, br.ObjectId)
the command line editor will also support lisp command strings, so you can change:
doc.SendStringToExecute(("-bedit " + blkname), true, false, false)
to:
doc.SendStringToExecute("(command """-bedit""" """ & blkname & """ """""")",true ,false ,false)
example final string value: (command "-bedit" "CL" "")
or something like that.
I did something similar but did not use the block editor. Insert each block in an empty drawing, zoom extents, and export to wmf. WMFs were used as screen shots in word documents.
@JamieVJohnson2 wrote:First, stop the crash, wrap ALL of it in a (vb.net)
Try
'all your code
Catch ex as SYSTEM.Exception
MsgBox(ex.tostring)
End Try
I don't get this line:
ed.WriteMessage(""& vbLf&"Block Name: \""{0}\"" Object ID: \""{1}\""", br.Name, br.ObjectId)
the command line editor will also support lisp command strings, so you can change:
doc.SendStringToExecute(("-bedit " + blkname), true, false, false)
to:
doc.SendStringToExecute("(command """-bedit""" """ & blkname & """ """""")",true ,false ,false)
example final string value: (command "-bedit" "CL" "")
or something like that.
While the command typed into AutoCAD works great for some reason after I execute the command I write in AutoCAD and it finishes I have to press enter in order to get the bedit to open the block in the editor. Then after I did hit enter and it opened the block editor AutoCAD completely crashed even after I slapped the try catch around everything.
which block are you trying to open some of them may be banned from opening.
What is the name of the block?
Is the block in an open transaction still? Close all transactions before opening bedit. (yep that means your going to have to collect your own object names and loop through them sepearately.) Get the idea? Transaction manager kills autocad. When autoCAD crashes, it usually develops a crash report. Open that zip file, and look at the xml based report file. then hunt for the section that states APP data. That will usually give out an actual explaination of why autocad crashed. If you get 'check top transaction' then all i said above applies.
jvj
That sounds like a great plan. You could also choose to print dwf. (standard viewer file used in Vault).
Thank you for your posts and recommendations. I have switched my code to open the a separate drawing and copy the blocks over. From there I was doing to load a block at a time into model space from the new drawing, take my screenshot, clear the modelspace block table and then move onto the next block. Does this even sound feasible?
I am currently hitting an error with my code...
"Operation is not valid due to the current state of the object."
Here is the latest revision of my code thus far.