Obtaining Block Insertion Point Using VB.Net

Obtaining Block Insertion Point Using VB.Net

mgorecki
Collaborator Collaborator
5,759 Views
3 Replies
Message 1 of 4

Obtaining Block Insertion Point Using VB.Net

mgorecki
Collaborator
Collaborator

Hi, I was hoping someone could help me out with this one.  I've searched this forum and some others and would just like to find some information on how to extract the insert point coordinate from a block reference inside the active drawing.  I have code that will select the blocks within a window, but now I need to get each block and extract it's insertion point.  Just for reference, here's my code so far:

\\code\\

 ' Create a selection set filter for blocks

Dim bgaFilter(0) As DatabaseServices.TypedValue

New DatabaseServices.TypedValue(DatabaseServices.DxfCode.Start, "INSERT")

Dim bgaSSFilter As New EditorInput.SelectionFilter(bgaFilter)

 

' Select all of the ballpads blocks using a window

myPSR = myEd.SelectWindow(ssWindowPnt1, ssWindowPnt2, bgaSSFilter)

myTransMan = myDWG.TransactionManager

myTrans = myTransMan.StartTransaction

If Not IsNothing(myPSR.Value) Then

bgaSelectionSet = myPSR.Value

bgaObjIDs = New DatabaseServices.ObjectIdCollection(bgaSelectionSet.GetObjectIds)

For Each bgaObjID In bgaObjIDs

   myEnt = bgaObjID.GetObject(OpenMode.ForRead)

\\code\\

 

I'm still learning, so my code above might not be right (if not, feel free to correct).  I also think my downfall is not knowing which objects have what modifiers (like myPSR.Value.  Value is on the list of things to get from myPSR).  Is there a place that has lists of these for reference?  I read somewhere a block reference has ".Location" or ".Position", but I cannot find it, probably because I couldn't get my block entity to be the right kind of object.

 

Anyway, I appreciate your help.

Thanks,

Mark

0 Likes
Accepted solutions (1)
5,760 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

You were almost there: you need to cast the object returned by GetObject(...) to BlockReference.

 

If your VB.NEt environment does not set "Option Strict On", you can

 

Dim dic As New Dictionary(of ObjectId, Point3d)()

For Each bgaObjID In bgaObjIDs

  ''VB.NET converts DBObject to BlockReference for you

  Dim myBlk As BlockReference=bgaObjID.GetObject(...)

  dic.Add(bgaObjID,myBlk.Position)

Next

If "Option Strict" is On, you need explicitly convert to BlockReference type

For Each bgaObjID In bgaObjIDs

  ''VB.NET converts DBObject to BlockReference for you

  Dim myBlk As BlockReference=CType(bgaObjID.GetObject(...),BlockReference)

  dic.Add(bgaObjID,myBlk.Position)

Next

Of course, the code is based on the fact you used filter to get the SelectionSet. Anything in the selectionset must be a BlockReference. Otherwise, you'd want to use TryCast() and test if the casting returns nothing to get a BlockReference object.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

mgorecki
Collaborator
Collaborator

Hello Norman,

Thank you for your help.  This seems to work.  Although I have found an interesting thing with AutoCad selections.  The selection set only consisted of the blocks that were visible in the AutoCAD screen.  Any block still in the selection window, but not in the viewable area was not selected.  I had this issue long ago with a LiSP routine.  I had to use a "Zoom Extents" prior to getting the selection set.  Do you know how I could do the same in VB.net? 

0 Likes
Message 4 of 4

norman.yuan
Mentor
Mentor
Accepted solution

Yes, that is an AutoCAD programming trap many programmers may fall in: when call Editor.SelectWindow()SelectCorssingWindow()/SelectPolygon()/SelectCrossingPolygon()... the window/polygon MUST be in the visible portion of the view area., because these methods do exactly as user manually does on sccreen picking window/polygon.

 

So, if your code supplies a window/polygon poinst to these methods, your code also needs to make sure the window/polygon is visisble on the screen. You probably need to do a zoomin gto the window/polygon before calling the Selectxxxxx() method.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes