Keep Block Selected after insert - BlockExplodeHelper.cs

Keep Block Selected after insert - BlockExplodeHelper.cs

SRSDS
Advisor Advisor
786 Views
6 Replies
Message 1 of 7

Keep Block Selected after insert - BlockExplodeHelper.cs

SRSDS
Advisor
Advisor

Hi, does anyone have Tony's BlockExplodeHelper class?

 

In particular I need to keep a block selected after it is inserted. For some reason the ObjectModified event is triggered after my insertion code that deselects it. The BlockExplodeHelper might have a solution.

ed.SetImpliedSelection(psr.Value)

 

Otherwise can anyone suggest a solution?

0 Likes
787 Views
6 Replies
Replies (6)
Message 2 of 7

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

sorry I don't have the libraryyou mentioned. But to get a blockreference gripped after it is inserted I see no problem (without having a look to your code). Look my sample (a block 'TEST' has to exist) it grips after having inserted the block.

 

   Public Shared Function insertBlRefAndGrip() As Boolean
      Dim tRetVal As Boolean = False

      Dim tAcadDoc As Document = Nothing
      Dim tAcadDocLock As DocumentLock = Nothing
      Dim tTrAct As Transaction = Nothing

      Try
         tAcadDoc = Application.DocumentManager.MdiActiveDocument
         tAcadDocLock = tAcadDoc.LockDocument
         tTrAct = tAcadDoc.TransactionManager.StartTransaction

         Dim tBlTab As BlockTable = CType(tTrAct.GetObject(tAcadDoc.Database.BlockTableId, OpenMode.ForRead), BlockTable)
         'check if BlockDef "TEST" exists and is valid
         If tBlTab.Has("TEST") Then
            Dim tBlDefID As ObjectId = tBlTab.Item("TEST")
            If (tBlDefID.IsValid) AndAlso (Not tBlDefID.IsErased) Then

               'get current space to insert the blockref
               Dim tSpace As BlockTableRecord = CType(tTrAct.GetObject(tAcadDoc.Database.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)

               'insert blockref
               Dim tBlRef As BlockReference = New BlockReference(Point3d.Origin, tBlDefID)
               Dim tBlRefID As ObjectId = tSpace.AppendEntity(tBlRef)
               tTrAct.AddNewlyCreatedDBObject(tBlRef, True)
               tTrAct.Commit()

               'grip blockref
               Dim tObjIDs() As ObjectId = {tBlRefID}
               tAcadDoc.Editor.SetImpliedSelection(tObjIDs)

               tRetVal = True
            Else
               Call MsgBox("Block 'TEST' is purged")
            End If
         Else
            Call MsgBox("Block 'TEST' not defined")
         End If

      Catch ex As Exception
         Call MsgBox("Error occured!" & vbNewLine & ex.Message)
      Finally
         If tTrAct IsNot Nothing Then tTrAct.Dispose() : tTrAct = Nothing
         If tAcadDocLock IsNot Nothing Then tAcadDocLock.Dispose() : tAcadDocLock = Nothing
      End Try

      Return tRetVal
   End Function

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 7

SRSDS
Advisor
Advisor

Thanks Alfred,

 

I've worked out what the problem was.

The block I am inserting has constrained dimensions and for the dimensions to be kicked into position I need regen.

Application.DocumentManager.MdiActiveDocument.SendStringToExecute("DIMREGEN ", True, False, False)
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("REGEN ", True, False, False)

 

Adding the SetImpliedSelection() after the regen I can see that it is selected but only for a fraction of a second before being deselected. Don't know why.

 

I might have to live with it.

0 Likes
Message 4 of 7

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> Adding the SetImpliedSelection() after the regen [...]

Wrong attempt to solve it, because the SendCommand in your could runs asynchronous (I assume that all yoru code was started via a command). So you set the commands "DIMREGEN" and "REGEN" in top of the command-stack, then you did the SetImpliedSelection .... then your command finished ==> and now the stack ships the buffered commands to the command-line.

 

In other words your REGEN's where done after your code, and so after you SetImpliedSelection.

 

- alfred -

 

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 7

SRSDS
Advisor
Advisor

It seems to be the same effect if added before or after the regen.

If I remove the regen it retains the selection.

Code is started from a pallette button.

0 Likes
Message 6 of 7

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> It seems to be the same effect if added before or after the regen.

Yes, you are right and I try do describe it better.

 

Within your code you wrote:

SendCommand DIMREGEN

SendCommand REGEN

SetImpliedSelection ...

 

AutoCAD starts

a) your command

     a1) add "DIMREGEN" to the command-stack

     a2) add "REGEN" to the command-stack

     a3) SetImpliedSelection

b) your command finished

c) start "DIMREGEN" from command-stack

d) start "REGEN" from command-stack

 

So whereever you do the SetImpliedSelection within your running command, it will always start before the command "REGEN" will be executed.

 

Hope it's more clear described now 😉

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 7 of 7

SRSDS
Advisor
Advisor

Sorry Alfred,

 

I can't seem to find out how to organise commands in the command stack . This is the first I've heard of it and can't seem to find any information on the forum. Great to hear there is a solution though and really appreciate the help.

0 Likes