How to access the position of an specific entity in a block using VBA

How to access the position of an specific entity in a block using VBA

ilovejingle
Enthusiast Enthusiast
4,927 Views
3 Replies
Message 1 of 4

How to access the position of an specific entity in a block using VBA

ilovejingle
Enthusiast
Enthusiast

 

I have a drawing with many block references of different block.

each block reference contains several  3D solids

one of the 3d solids is of my interests. it is a 3D solid box

I want to find the position of the 3D solid box in each of the block references. namly the 3D coordinates of it  reference to the block insertion point or the WCS zero.

is there a way to do it using VBA ?

 

 

Regards

 

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

norman.yuan
Mentor
Mentor
Accepted solution

You need to loop through entities in the block definition(AcadBlock) to find the entity in interest, and its position to the block's base point (0,0,0). Then for each of the block references, factor in the block refernece's insertion point.

 

The pseudo code would look like:

 

'' Get the position of the Solid inside the block definition

Dim blk as AcadBlock

set blk=ThisDrawing.Blocks("myBlock")

 

Dim ent as AcadEntity

Dim position(0 to 2) As Double

For Each ent in blk

  If TypeOf ent Is AcadSolid And [some other condition to make sure it is the target, if necessary] Then

    position=[Get a set of coordinate from the Solid, such as one of its corner...]

    Exit For

  End If

Next

 

'' Get the positions related to the drawing's 0, 0,0 (such as ModelSpace's World Origin:

Dim bref As AcadBlockReference

Dim pos() As Variant

Dim i As Ineter

For Each ent in ThisDrawing.ModelSpace

  If TypeOf ent Is AcadBlockrefernece Then

    Set bref=ent

    If UCase(bref.EffectiveName)=UCase("myBlock") Then

      ReDim pos(i)

      pos(i)(0)=bref.InsertionPoint(0) + position(0)

      pos(i)(1)=bref.InsertionPoint(1) + position(1)

      pos(i)(2)=bref.InsertionPoint(2) + position(2)

      i=i+1

    End if

  End if

Next

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4

ilovejingle
Enthusiast
Enthusiast

Thanks very much, I didn't know it can loop though all the elements in a block definition, now it works perfectly.

Message 4 of 4

arazaSNXR7
Participant
Participant

Thank you so much!

0 Likes