VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Add an entity to a block

8 REPLIES 8
Reply
Message 1 of 9
Anonymous
292 Views, 8 Replies

Add an entity to a block

Hi,

I am struggling to add an entity to a block definition. I have the following
code but the circle does not get added:

Sub TTest()
Dim blkMyBlock As AcadBlock
Dim dCenter(0 To 2) As Double
Dim dRadius As Double
dCenter(0) = 0#: dCenter(1) = 0#: dCenter(2) = 0#
dRadius = 100#
For Each blkMyBlock In ThisDrawing.Blocks
If blkMyBlock.Name = "TEST" Then
Set blkMyBlock = blkMyBlock.AddCircle(dCenter, dRadius)
End If
Next blkMyBlock
End Sub

Any assistance will be appreciated.

Regards

Sean Bartleet
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

You need to return the AddCircle method to a BlockReference object. Also
consider setting the BlockDef via
Set blkMyBlock = ThisDrawing.Blocks.item("TEST') rather than iterating
through all block def's.
--
John Goodfellow
irtfnm
use john at goodfellowassoc dot com


"Sean Bartleet" wrote in message
news:BB699F04BDCCBF74182F50F8776C0F04@in.WebX.maYIadrTaRb...
> Hi,
>
> I am struggling to add an entity to a block definition. I have the
following
> code but the circle does not get added:
>
> Sub TTest()
> Dim blkMyBlock As AcadBlock
> Dim dCenter(0 To 2) As Double
> Dim dRadius As Double
> dCenter(0) = 0#: dCenter(1) = 0#: dCenter(2) = 0#
> dRadius = 100#
> For Each blkMyBlock In ThisDrawing.Blocks
> If blkMyBlock.Name = "TEST" Then
> Set blkMyBlock = blkMyBlock.AddCircle(dCenter, dRadius)
> End If
> Next blkMyBlock
> End Sub
>
> Any assistance will be appreciated.
>
> Regards
>
> Sean Bartleet
>
>
Message 3 of 9
Anonymous
in reply to: Anonymous

Hi,

Thanks for the top to reference the block directly.

But, I am now confused. What I want to do is add a circle to the block
definition and update all instances of that block in the drawing.

Help tells me that a "BlockRef object" is "An instance of a block definition
inserted into a drawing." I interpret this to be a reference to the block
definition. Help also does not list an add method to this class.

Help tells me that a "Block object" is "A block definition containing a name
and a set of objects" or "A simple block is a collection of objects you can
associate together to form a single object, or block definition." Which had
an add method.

Am I wrong in saying that I need to use the add method to add a circle to
the block definition?

The following code gives me an type mismatch error on the "Set blkMyBlock =
blkMyBlock.AddCircle(dCenter, dRadius)" line.

Sub Test1()
Dim blkMyBlock As AcadBlock
Dim dCenter(0 To 2) As Double
Dim dRadius As Double
dCenter(0) = 0#: dCenter(1) = 0#: dCenter(2) = 0#
dRadius = 100#
Set blkMyBlock = ThisDrawing.Blocks.Item("TEST")
Set blkMyBlock = blkMyBlock.AddCircle(dCenter, dRadius)
End Sub

Any suggestions?

Sean Bartleet

"John Goodfellow" wrote in message
news:EAA39B6B6B2C3BF9622D875DCBCB3FAA@in.WebX.maYIadrTaRb...
> You need to return the AddCircle method to a BlockReference object. Also
> consider setting the BlockDef via
> Set blkMyBlock = ThisDrawing.Blocks.item("TEST') rather than iterating
> through all block def's.
> --
> John Goodfellow
> irtfnm
> use john at goodfellowassoc dot com
Message 4 of 9
Anonymous
in reply to: Anonymous

Sean,
Try this
Sub Test1()
Dim blkMyBlock As AcadBlock
Dim dCenter(0 To 2) As Double
Dim dRadius As Double
dCenter(0) = 0#: dCenter(1) = 0#: dCenter(2) = 0#
dRadius = 100#
Set blkMyBlock = ThisDrawing.Blocks.Item("TEST")

blkMyBlock.AddCircle dCenter, dRadius
ThisDrawing.Regen acActiveViewport

Set blkMyBlock = Nothing
End Sub

hth
Mark

"Sean Bartleet" wrote in message
news:398939D06D8854BE93360B5954840704@in.WebX.maYIadrTaRb...
> Hi,
>
> Thanks for the top to reference the block directly.
>
> But, I am now confused. What I want to do is add a circle to the block
> definition and update all instances of that block in the drawing.
Message 5 of 9
Anonymous
in reply to: Anonymous

Hi,

Thanks, this worked great.

Any idea as to why the syntax is is not shown in the help like that?

What is the difference between
blkMyBlock.AddCircle dCenter, dRadius
and
Set blkMyBlock = blkMyBlock.AddCircle(dCenter, dRadius)

How am I supposed to know when to use what syntax?

Thanks again for the help.

Sean

"Mark Propst" wrote in message
news:740A1B93D9964EFC141F7018D9A1F05E@in.WebX.maYIadrTaRb...
> Sean,
> Try this
> Sub Test1()
> Dim blkMyBlock As AcadBlock
> Dim dCenter(0 To 2) As Double
> Dim dRadius As Double
> dCenter(0) = 0#: dCenter(1) = 0#: dCenter(2) = 0#
> dRadius = 100#
> Set blkMyBlock = ThisDrawing.Blocks.Item("TEST")
>
> blkMyBlock.AddCircle dCenter, dRadius
> ThisDrawing.Regen acActiveViewport
>
> Set blkMyBlock = Nothing
> End Sub
>
> hth
> Mark
Message 6 of 9
Anonymous
in reply to: Anonymous

"Sean Bartleet" wrote in message
news:2FDB3F393C5C8C503B30877B7D0D3054@in.WebX.maYIadrTaRb...
> Hi,
>
> Thanks, this worked great.
>
> Any idea as to why the syntax is is not shown in the help like that?
>
well, call it paranoia but after trying to learn programming on my own for
more than a few years now, (and before learning of these ngs which are a god
send to anyone who wants to learn), I have come to the conclusion from all
the typos, unavailable, unclear or incomplete explanations, let alone the
downright wrong explanations, that help files were only created to deter the
slothful, lazy, and anyone with less than a 5000 iq from ever learning to
program...:-)~
(but maybe it's just me!)

> What is the difference between
> blkMyBlock.AddCircle dCenter, dRadius
> and
> Set blkMyBlock = blkMyBlock.AddCircle(dCenter, dRadius)

the "Set" keyword?directive? (if that's the right term - probably not) is a
'command' that creates an object, an instance of a class. It allocates a
memory location and then stores the object you create in the symbol you
provide it, in your case blkMyBlock. Once you have that object created in
memory you have all the methods and properties which that object possesses
at your command. For the duration of the life of your object(scope) you can
manipulate, read or write, the various properties and exercise the methods
belonging to that object. You don't need the "Set" in order to utilize any
of those 'attributes' of the object. The "Set" is only needed to create the
object in the first place, and then at the end of your program, in most
cases you want to Set blkMyBlock = Nothing, in order to free up the memory
location allocated in your original Set blkMyBlock = "create or get block
object." and allow that memory slot to be used by ongoing operations
(prevent memory 'leaks')
I'm far from an expert on this and hopefully some one who really knows what
they're talking about can correct *my* inaccuracies and deficiencies of
explanation - lest I become known as little better than an errant help file!
:-)~
have fun
Mark
Message 7 of 9
wivory
in reply to: Anonymous

Sean,



I don't blame you (and Mark) for being confused about this, but I'll try to explain.



Methods (such as AddCircle) typically both *do* something (eg add a circle) and *return* something (eg the newly added circle object). If you need the returned object for further processing then the left side of the statement needs to assign the returned object to a variable.


Set MyNewLine = ThisDrawing.ModelSpace.AddLine(pt1, pt2)

If you don't need the returned object then you can omit the assignment part of the statement. The parentheses around the arguments are also omitted in this circumstance.


ThisDrawing.ModelSpace.AddLine pt1, pt2

Now in your case you are using an AddCircle method, and the Help says that the return value from this method is "The newly created Circle object". If you are going to assign this object to a variable it needs to be of type AcadCircle, whereas you were trying to assign it to a variable of type AcadBlock (ie blkMyBlock). It would seem from the context that you only wanted to *do* the method and didn't need the *return* value which is why Mark's solution worked for you.



Hope this helps.



Regards



Wayne Ivory

IT Analyst Programmer

Wespine Industries Pty Ltd
Message 8 of 9
Anonymous
in reply to: Anonymous

Thank you for fixing my botched misinformation.
Man was I off on that one!

"wivory" wrote in message
news:f14c1c3.5@WebX.maYIadrTaRb...
> Sean,
>
> I don't blame you (and Mark) for being confused about this, but I'll try
to explain.
>
> Methods (such as AddCircle) typically both *do* something (eg add a
circle) and *return* something (eg the newly added circle object). If you
need the returned object for further processing then the left side of the
statement needs to assign the returned object to a variable.
>
>
> Set MyNewLine = ThisDrawing.ModelSpace.AddLine(pt1, pt2)
>
> If you don't need the returned object then you can omit the assignment
part of the statement. The parentheses around the arguments are also omitted
in this circumstance.
>
>
> ThisDrawing.ModelSpace.AddLine pt1, pt2
>
> Now in your case you are using an AddCircle method, and the Help says that
the return value from this method is "The newly created Circle object". If
you are going to assign this object to a variable it needs to be of type
AcadCircle, whereas you were trying to assign it to a variable of type
AcadBlock (ie blkMyBlock). It would seem from the context that you only
wanted to *do* the method and didn't need the *return* value which is why
Mark's solution worked for you.
>
> Hope this helps.
>
> Regards
>
> Wayne Ivory
> IT Analyst Programmer
> Wespine Industries Pty Ltd
>
>
Message 9 of 9
Anonymous
in reply to: Anonymous

Hi,

Thanks Mark for the initial explanation and Wayne for the clarification.
This has helped a lot.

Regards.

Sean

"wivory" wrote in message
news:f14c1c3.5@WebX.maYIadrTaRb...
> Sean,
>
> I don't blame you (and Mark) for being confused about this, but I'll try
to explain.
>
> Methods (such as AddCircle) typically both *do* something (eg add a
circle) and *return* something (eg the newly added circle object). If you
need the returned object for further processing then the left side of the
statement needs to assign the returned object to a variable.
>
>
> Set MyNewLine = ThisDrawing.ModelSpace.AddLine(pt1, pt2)
> If you don't need the returned object then you can omit the assignment
part of the statement. The parentheses around the arguments are also omitted
in this circumstance.
>
>
> ThisDrawing.ModelSpace.AddLine pt1, pt2
> Now in your case you are using an AddCircle method, and the Help says that
the return value from this method is "The newly created Circle object". If
you are going to assign this object to a variable it needs to be of type
AcadCircle, whereas you were trying to assign it to a variable of type
AcadBlock (ie blkMyBlock). It would seem from the context that you only
wanted to *do* the method and didn't need the *return* value which is why
Mark's solution worked for you.
>
> Hope this helps.
>
> Regards
>
> Wayne Ivory
> IT Analyst Programmer
> Wespine Industries Pty Ltd
>

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost