Moving Blocks on the Fly

Moving Blocks on the Fly

Anonymous
Not applicable
1,662 Views
7 Replies
Message 1 of 8

Moving Blocks on the Fly

Anonymous
Not applicable
Hello,

I am attempting to move a block inside model space using VBA. I know the name of the block
so it can be hard coded into the function, and I can locate the block, but when I go to use the
.move command things get difficult. Eventaully I would like to have the code sellect several
blocks at the same time and allow the user to select "Point2" on the fly similar to what happens
when you use the move command from AutoCAD

Private Sub CommandButton3_Click()
Dim oBlock As AcadBlock
Dim oBlocks As AcadBlocks
Dim BlockName As String
Dim Point1(0 To 2) As Double
Dim Point2(0 To 2) As Double

BlockName = "EPF 245 CL2 ARR3 TOP 1"
Point1(0) = 0: Point1(1) = 0: Point1(2) = 0
Point1(0) = 100: Point1(1) = 100: Point1(2) = 0

Set oBlocks = ThisDrawing.Blocks
For Each oBlock In oBlocks
If oBlock.Name = BlockName Then
oBlock.Move Point1, Point2
End If
Next
End Sub

As always any help would be greatly appreciated!
0 Likes
1,663 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Does your "Move Block on the fly" mean the ghost
image of selected block(s) moving along the cursor while user is trying to
select "Move To" point? If so, with VBA, you can forget it. If you can program
with ObjectARX (C++) or .NET API, then yes, you can use Jig class to achieve
that, quite easily, actually.

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hello,
I am attempting to move a block inside model space using VBA. I know the name
of the block so it can be hard coded into the function, and I can locate the
block, but when I go to use the .move command things get difficult. Eventaully
I would like to have the code sellect several blocks at the same time and
allow the user to select "Point2" on the fly similar to what happens when you
use the move command from AutoCAD Private Sub CommandButton3_Click() Dim
oBlock As AcadBlock Dim oBlocks As AcadBlocks Dim BlockName As String Dim
Point1(0 To 2) As Double Dim Point2(0 To 2) As Double BlockName = "EPF 245 CL2
ARR3 TOP 1" Point1(0) = 0: Point1(1) = 0: Point1(2) = 0 Point1(0) = 100:
Point1(1) = 100: Point1(2) = 0 Set oBlocks = ThisDrawing.Blocks For Each
oBlock In oBlocks If oBlock.Name = BlockName Then oBlock.Move Point1, Point2
End If Next End Sub As always any help would be greatly
appreciated!
0 Likes
Message 3 of 8

Anonymous
Not applicable
Hi cperrin,

It's a little confusing, but an AutoCAD block in VBA is not in drawing,
but merely the definition of the block in the block table.

When a block is inserted in the drawing, it becomes a blockreference.

So, replace all the AcadBlock with AcadBlockReference.

As you can see Autodesk have garbled your code and I'm not going to try
to parse it to look for other issues.

If you place the code between code markers like this:


Place code here


then it will retain its formatting.

Regards


Laurie Comerford

cperrin wrote:
> Hello, I am attempting to move a block inside model space using VBA. I
> know the name of the block so it can be hard coded into the function,
> and I can locate the block, but when I go to use the .move command
> things get difficult. Eventaully I would like to have the code sellect
> several blocks at the same time and allow the user to select "Point2" on
> the fly similar to what happens when you use the move command from
> AutoCAD Private Sub CommandButton3_Click() Dim oBlock As AcadBlock Dim
> oBlocks As AcadBlocks Dim BlockName As String Dim Point1(0 To 2) As
> Double Dim Point2(0 To 2) As Double BlockName = "EPF 245 CL2 ARR3 TOP 1"
> Point1(0) = 0: Point1(1) = 0: Point1(2) = 0 Point1(0) = 100: Point1(1) =
> 100: Point1(2) = 0 Set oBlocks = ThisDrawing.Blocks For Each oBlock In
> oBlocks If oBlock.Name = BlockName Then oBlock.Move Point1, Point2 End
> If Next End Sub As always any help would be greatly appreciated!
0 Likes
Message 4 of 8

Anonymous
Not applicable
Yeah, I was hoping to have the ghost image of the selected blocks in there as well. I'm afraid
that anything much above VBA (and including VBA on some days) is above my head, I took a
C++ class, but that was too long ago to do me any good.

I am also looking into "ThisDrawing.SendCommand ("move" & vbCr & ...) which I believe might
allow me to do what I want, but I'm having trouble selecting the block either prior to during the
move command.
0 Likes
Message 5 of 8

Anonymous
Not applicable
Alright, I'm not sure what you mean by "Place code between code markers"?
Also, I'm not sure why it is not garbled on my computer? I will try attaching
the code at a .txt file to see if that helps?

If I replace AcadBlock with AcadBlockReference then I get type mismatch errors
when I try to relate the block back to a reference for the move?
0 Likes
Message 6 of 8

Anonymous
Not applicable
Hi cperrin,

Obviously I did not make myself clear enough.

In VBA you CANNOT MOVE a block. The block is a definition in the drawing
block table and it does not have a drawing location which can be moved.

You MUST work with the Block references which are the objects in the
drawing.

Have a look at the code below.


Private Sub CommandButton3_Click()
Dim oblockRef As AcadBlockReference
Dim oEnt As AcadEntity
' Dim oBlock As AcadBlock
' Dim oBlocks As AcadBlocks
Dim BlockName As String
Dim Point1(0 To 2) As Double
Dim Point2(0 To 2) As Double
Dim i As Integer
UserForm1.Hide '<--- You can use "Me.Hide". It has less typing
BlockName = "EPF 245 CL2 ARR3 TOP 1"
Point1(0) = 0: Point1(1) = 0: Point1(2) = 0
Point1(0) = 100: Point1(1) = 100: Point1(2) = 0

' Set oBlocks = ThisDrawing.Blocks
'<--- This is not the best way but works
' It is better to create a selection set of blockreferences
' and loop through it
For Each oEnt In ThisDrawing.ModelSpace

If TypeOf oEnt Is AcadBlockReference Then
Set oblockRef = oEnt
If oblockRef.Name = BlockName Then
oblockRef.Move Point1, Point2
End If '
End If '
' If oBlock.Name = BlockName Then
' oBlock.Move Point1, Point2
' End If
Next

' For Each oBlock In oBlocks
' If oBlock.Name = BlockName Then
' oBlock.Move Point1, Point2
' End If
' Next
End Sub



Regards


Laurie Comerford

cperrin wrote:
> Alright, I'm not sure what you mean by "Place code between code
> markers"? Also, I'm not sure why it is not garbled on my computer? I
> will try attaching the code at a .txt file to see if that helps? If I
> replace AcadBlock with AcadBlockReference then I get type mismatch
> errors when I try to relate the block back to a reference for the move?
>
0 Likes
Message 7 of 8

Anonymous
Not applicable
Laurie,

Thanks for your help, that will work for now, still seems like there should
be some way of keeping the ghost image during the move? Do you know
anything about using: ThisDrawing.SendCommand ("move"... It would appear
that this could work if there is someway of selecting multiple block references
in the drawing from VBA?

Anyway thanks again,
Chris
0 Likes
Message 8 of 8

Anonymous
Not applicable
Hi Chris,

VBA does not have a way of showing ghost images while moving objects.
The best way to see the objects while moving is to use the supplied
AutoCAD Move command from the Edit menu. If it is critical for your
processes, check in the .NET NG whether you can do it with .NET, or use
lisp.

In programming you have to determine the start and end point of moves
before invoking the move command.

Generally in VBA you would move the block by changing its insertion
point rather than "move"ing it.

oBlockReference.InsertionPoint = Pt

Using "ThisDrawing.Sendcommand" in VBA should only be used as a last
resort and as the last item in the code before some user intervention.

VBA code mostly continues running while the SendCommand action is in
process and this can create hard to resolve issues.

cperrin wrote:
> Laurie, Thanks for your help, that will work for now, still seems like
> there should be some way of keeping the ghost image during the move? Do
> you know anything about using: ThisDrawing.SendCommand ("move"... It
> would appear that this could work if there is someway of selecting
> multiple block references in the drawing from VBA? Anyway thanks again,
> Chris
0 Likes