Classic Insert Block dialog

grobnik
Collaborator
Collaborator

Classic Insert Block dialog

grobnik
Collaborator
Collaborator

Hi to everybody,

I searching a way to insert a block in a drawing but without inserting it as object, I just want to be ready for inserting as block reference by next ThisDrawing.ModelSpace.InsertBlock method.

I'll try to explain better:

- I'm creating a new drawing, which a VBA procedure require the use of a block that I can insert as dwg, but due to I don't know before the path I would like to browse my computer and find the block as dwg, a possibility could be the use of classic insert dialog box which foreseen the browsing option, but I don't know how to recall by VBA.

grobnik_0-1612689971112.png

 

- As second option may I recall the above dialog by "send command" but I don't like so much.

Any Idea ?

thank you

0 Likes
Reply
Accepted solutions (1)
1,232 Views
4 Replies
Replies (4)

norman.yuan
Mentor
Mentor
Accepted solution

If I am not mistaken on what you asked: you want to runa VBA program against a newly created drawing (be it created by the same VBA program, or not) to add a block into the drawing from a block *.dwg file, but only the block's definition, not block reference. The purpose, according to you is that another VBA program later may actually need to actially insert a block reference of this block definition, so you want to make sure the block definition pre-exists before the other VBA program runs (maybe the users running the other VBA are those who are much less skilled and do not know how to browse folders to find correct block file for inserting?).

 

Anyway, if you only need the block definition, not the reference, being inserted from a block DWG file, there are 2 issues here: find/locate correct block DWG file and insert the block as definition, not reference.

 

For the latter issue: VBA/COM API does not provide a way to insert block definition, as .NET API does. I think using ModelSpace[PaperSpace].InsertBlock() to actually insert a block reference from a block DWG file (thus the block definition is brought in) and then simply delete the inserted block reference would be simple and straightforward way.

 

As for finding/locating block DWG file name, you really do not need to use the classical inserting dialog. You simply use an OpenFile dialog to let user select a block *.dwg file. Ideally, your code should search all support paths of current user's profile: a well/reasonably established AutoCAD environment in any office should have its AutoCAD resources, including blocks, correctly covered by AutoCAD's support paths. Now that you are writing code, you should have a ready-to-use function that searches supporting paths for a given file name, something like:

 

Public Function FindFile(fileName As String) As String

 

In this function you would use VB's Dir() function to go through each support path and looking for particular file name and returns a fully pathed file name, if found or empty string, if not found.

 

You only pops up the OpenFile dilog if the support path search turns up empty, if the said VBA is run by user.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

grobnik
Collaborator
Collaborator

Hi @norman.yuan , thank you for your messagge.

I'm little bit complicated on my project, however I guess I skipped the issue, adding to the VBA code a block definition without inserting (it was not my first idea, but seems the simply solution).

There is a procedure that could be applied on existing or new drawing, but of course need the availability of a specific block with an attribute. Block it's mainly composed by a point and related number (represented by the block attribute).

Of course in a new drawing there is any kind of block, so the procedure cannot insert a new block if not inside the drawing. So my first idea was the check of block inside the drawing, if not then open "insert" dialog for search the block as dwg stored somewhere in the computer, but seems more complicated.

So I tried to approach the creation of simply block definition, if not exist, and later I'll be able to insert as per mainly procedure require. 

If could be useful for somebody below the code.

Private Sub BlockCheck()
'define variables
    On Error Resume Next
    Dim dblOrigin(0 To 2) As Double
    
    Dim sysVarName As String
    Dim sysVarData As Variant
    Dim DataType As Integer
        
    Dim intData As Integer
    sysVarName = "PDMODE"
    intData = 35
    sysVarData = intData    ' Integer data
    ThisDrawing.SetVariable sysVarName, sysVarData
    
    sysVarName = "PDSIZE"
    intData = 1
    sysVarData = intData    ' Integer data
    ThisDrawing.SetVariable sysVarName, sysVarData
    
    Dim CurBlock As AcadBlock
    Set CurBlock = ThisDrawing.Blocks.Item("MyPoint")
    If Err <> 0 Then
    
    Dim blockObj As AcadBlock
    Dim var As String
    var = "MyPoint"
    
    Dim insertionPnt(0 To 2) As Double
    insertionPnt(0) = 2
    insertionPnt(1) = 2
    insertionPnt(2) = 0

    Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, var)
    
    Dim AttributeObj As AcadAttribute
    
    Dim height As Double
    Dim mode As Long
    Dim prompt As String
    Dim tag As Variant
    Dim value As String
    
    height = 1
    mode = acAttributeModeNormal
    prompt = "POINT_NUMBER"
    tag = "POINT"
    value = "1"
    Dim InsertionPoint(0 To 2) As Double
    
    Dim MyPointObj As AcadPoint
    InsertionPoint(0) = 0
    InsertionPoint(1) = 0
    InsertionPoint(2) = 0

    Set MyPointObj = blockObj.AddPoint(InsertionPoint)
    
    Dim BlockRefObj As AcadBlockReference
    
    InsertionPoint(0) = 1.5
    InsertionPoint(1) = 1.5
    InsertionPoint(2) = 0

    Set AttributeObj = blockObj.AddAttribute(height, mode, prompt, InsertionPoint, tag, value)
    
    Dim insertionPt(0 To 2) As Double

    'insertionPt(0) = 0
    'insertionPt(1) = 0
    'insertionPt(2) = 0
    
    'Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPt, var, 1#, 1#, 1#, 0)
   ' MyAtt = BlockRefObj.GetAttributes
   ' MyAtt(0).Alignment = acAlignmentMiddleCenter

   ' MyAtt(0).InsertionPoint = InsertionPoint
   ' BlockRefObj.Delete
    
    'ZoomExtents
    'MsgBox "This Block represents " & var
End If

End Sub

Perhaps the code it's not perfect, it's coming from several cut and paste, but seems to solve my issue.

Thank you.

 

Bye

0 Likes

Sea-Haven
Mentor
Mentor

A quicky fix is insert block at say 0,0 then erase the block, its definition will still be there so a following Insert will find it.

grobnik
Collaborator
Collaborator
Thank you for the suggestion but how to insert a block in an empty drawing for deleting it immediately after the insertion ? I guess I have to create a block definition, and later the whole procedure will insert a block reference. More or less as per above procedure.
Thank you
0 Likes