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

How to Open Block in Block Editor

8 REPLIES 8
Reply
Message 1 of 9
cjxmccac
9026 Views, 8 Replies

How to Open Block in Block Editor

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!

8 REPLIES 8
Message 2 of 9
JamieVJohnson2
in reply to: cjxmccac

send command to the command line: "-bedit " & strBlockName

 

jvj

jvj
Message 3 of 9
cjxmccac
in reply to: JamieVJohnson2

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);

Message 4 of 9
JamieVJohnson2
in reply to: cjxmccac

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.

jvj
Message 5 of 9
fieldguy
in reply to: cjxmccac

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.

Message 6 of 9
cjxmccac
in reply to: JamieVJohnson2


@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.

Message 7 of 9
JamieVJohnson2
in reply to: cjxmccac

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

jvj
Message 8 of 9
JamieVJohnson2
in reply to: fieldguy

That sounds like a great plan.  You could also choose to print dwf.  (standard viewer file used in Vault).

jvj
Message 9 of 9
cjxmccac
in reply to: JamieVJohnson2

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.

 

 

           DocumentCollection dm = Application.DocumentManager;
            Database sourceDB = dm.MdiActiveDocument.Database;
            try
            {
                // Collect the objectID's to be copied
                ObjectIdCollection blockIds = new ObjectIdCollection();
                Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = sourceDB.TransactionManager;
                using (Transaction tran = tm.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tm.GetObject(sourceDB.BlockTableId, OpenMode.ForRead, false);
                    foreach (ObjectId btrId in bt)
                    {
                        BlockTableRecord btr = (BlockTableRecord)tm.GetObject(btrId, OpenMode.ForRead, false);
                        if (!btr.IsAnonymous && !btr.IsLayout)
                            blockIds.Add(btrId);
                        btr.Dispose();
                    }
                }
                // Create the new Drawing to put the objects in
                Document acTempDoc = dm.Add("acad.dwt");
                acTempDoc.LockDocument();
                Database destDB = acTempDoc.Database;
                // Put the objects in the new drawing
                IdMapping mapping = new IdMapping();
                sourceDB.WblockCloneObjects(blockIds, destDB.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                
                // Draw the a block on the new drawing
                Autodesk.AutoCAD.DatabaseServices.TransactionManager tmDest = destDB.TransactionManager;
                using (Transaction tranDest = tmDest.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tranDest.GetObject(destDB.BlockTableId, OpenMode.ForRead);
                    foreach (ObjectId btrid in bt)
                    {
                        BlockTableRecord btr = (BlockTableRecord)tranDest.GetObject(btrid, OpenMode.ForRead);
                        foreach (ObjectId id in btr)
                        {
                            BlockReference brMS = new BlockReference(Point3d.Origin, id);
                            BlockTableRecord ms = (BlockTableRecord)tranDest.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                            ms.AppendEntity(brMS);
                            tranDest.AddNewlyCreatedDBObject(brMS, true);
                            tranDest.Commit();
                        }
                    }
                }
             }
            catch (System.Exception ex) 
            {
                Application.ShowAlertDialog(ex.Message);
            }

 

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