Hatch a Block Reference through VBA

Hatch a Block Reference through VBA

g_spoerer
Explorer Explorer
747 Views
3 Replies
Message 1 of 4

Hatch a Block Reference through VBA

g_spoerer
Explorer
Explorer

Hello,

 

i'm trying to hatch a dynamic block reference through VBA and i cant get it to work.

 

A summary of the routine is as follows:

 

---------------------------------------------------------------------------------------------------------------------------------

Dim BIMOBJ As AcadBlockReference

Set BIMOBJ = ThisDrawing.ModelSpace.InsertBlock(center, BN, 1, 1, 1, Ang)         --> (Dont mind those parameters, they work correctly, the block reference is inserted correctly)

 

Dim objhatch As AcadHatch

Set objhatch = ThisDrawing.ModelSpace.AddHatch(acHatchPatternTypePreDefined, "ANSI31", True, acHatchObject)

 

 

Dim OuterLim() As AcadEntity

ReDim OuterLim(0)

 

--- FROM HERE ON I DONT KNOW WHAT TO DO------


Set OuterLim(0) =BIMOBJ   -->>???      

 

Dim blockent As AcadEntity: Set blockent = BIMOBJ

Set OuterLim(0) =blockent   -->>???     

 

objhatch.AppendOuterLoop (OuterLim)     -->This is the part that is now working. I don tknow how to define a blockreference as OuterLoop

 

------------------------------------------------------------------------------------------------------------------------------------------------------

 

By the way, if I command-hatch manualy via autocad modelspace interface, selecting the blockreference, the hatch is done correctly and works great. 

 

Any Ideas?

Thanks!

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

norman.yuan
Mentor
Mentor
Accepted solution

Well, AutoCAD built-in commands, in most cases, are not a simple call to an API (COM API, in the case of VBA) method. Usually they are multiple API steps. For example, command "EXPLODE' is not equal to AcadBlockReference.Explode(), because for the latter, your code would be responsible to decide what to do with the original AcadBlockReference, which is "exploded".

 

It is similar in the case of Hatch command: with "Hatch" command you can choose a block reference as hatch loop/boundary, but the command does more things to actually obtain a valid boundary based on the selected block reference; while with AcadHatch.AppendInner[Outer]Loop() you must supply legit entities (Arc/Circle/Ellipse,Line, Polyline/SPline/Region, BUT NOT AcadBlockReference!).

 

So, you need to do your work (as the Hatch command does behind the scene): you must get a valid loop by analyzing the entities in block reference's definition to find out if you can obtain an closed boundary as a loop. It is definitely not an easy task to do with VBA, unless the block is extremely simple. If you do AutoCAD .NET API programming, it would a bit easier, I'd say.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

g_spoerer
Explorer
Explorer

Thanks! Actually i kept trying all night till a got something similar to what i wanted and similar to what you said:

I had to create or set a new AcadBlock Object wich contains the block reference object that i was dealing with. This is:

 

set BlockENT = ThisDrawing.Blocks(BIMOBJ.name) // with BlockENT an AcadBlock and BIMOBJ an AcadBLockreference.

 

This is the way to interact with elements inside a reference of a dynamic block.

Then i can "get into" the BlockENT and iterate to select the Polyline i needed as a hatch boundry, wich I can associate to OuterLoop. THis is:  Set OuterLim(0) =BlockEnt.Item(x)

 

In addition, block.Addhatch method must be used instead of ThisDrawing.Modelspace.AddHatch....

It works great, the only problem is that the hatch is created inside the block, and not on model space as hatch command does. This means I cant interact with it via in model space.  

If I use ThisDrawing.Modelspace.AddHatch.... method, it does not recognize the polyline inside the block as an outerloop, probably because it is not on modelspace directly.

0 Likes
Message 4 of 4

norman.yuan
Mentor
Mentor

You can explode the block reference (BOMOBJ.Explode()) to set a set of the element entities of the block defines. Then, you find the target entity or entities that forms the hatch boundary as the parameters passed to AcadHatch.AppendOuter[Inner]Loop(). You need to erase the other entities that are not the loop or part of the loop. This way, the hatch is created in the MOdel[PaperSpace, not inside the block definition.

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes