Author Topic: Total newby to the swamp... VB block insertion  (Read 33649 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4042
  • C3D user & customizer
Re: Total newby to the swamp... VB block insertion
« Reply #30 on: May 18, 2006, 08:15:04 PM »
view-> immediate window
or CTRL + G

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #31 on: May 18, 2006, 08:30:12 PM »
Bryco,

Thanks for the input. Right now our blocks are being inserted mostly via the "Image Tile Menu" as it is called in the CUI editor and there are also a few instances of some blocks being inserted via the menu pull downs. My dilema with the "Image Tile Menu" is that it has been around (legacy) for a long long time and I wonder if Autodesk is going to do away with it. Who knows on that one. Be that as it may or may not be, it is my thinking that the VBA app would be more likely to stick around for my lifetime anyway and it also gives a nice GUI for the users. Click Click shoot....

Since moving to 2006 and the new CUI the menu pull downs are very slow to respond. I have heard it is because we have our "company" customizations on a server. The "Image Tile Menu" still responds quickly enough. Also, when accessing the text style dialoge box and the layer box for the first time in a session, those too are very slow to respond...again... probably because our customizations are on our server. I have not completely explored that one.

As far as not using a "visual" of the block, I understand that too. However, we have some blocks that are "visually" specific. So, having a visual representation of the block is something I would like to have in the app. I think I can use the slide library we are already using for the "Image Tile Menu". I will cross that bridge when I come to it.

We also insert some of our more common blocks from toolbars. I.e., keynote symbols, revision deltas, etc. So. The block insertion lisp routine Kerry Brown wrote works for Toolbars, Image Tile Menus and Menu Pull Downs. So, I would still use that routine to insert from the Toolbars and Menu Pull Downs. The Image Tile Menu will stay and users can keep using that... until...and if.... Autodesk does away with Image Tile Menus. So, if they do get rid of it, the new VBA app for inserting the blocks will takes its place. Just trying to make sure there is some sort of GUI for the future. Plus, with the app it allows for a "Company" specific layout of the GUI.

I apologize for the long winded answer.

Thanks for the "view-immediate window" help. :-) Duhhhhhhhhh.... I am just a hack at this stuff. Been a drafter for a long time and I am definately nobodies programmer.

So, with that said..... Now I am going to try and plug the info from the coding Jeff is helping me with and see if I can actually make it do the insertion. hahahahahaaha

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #32 on: May 23, 2006, 02:18:23 PM »
Here I am again.....

The "InsertButton_Click" is code posted by Barry Clark earlier in this post. I have been hacking away at this part with no luck. This will create the layer but does nothing after that....namely the insertion.

All of the files I am using are attached in the zip and the code is posted below for quick viewing.

Can anyone give me a clue?

Code: [Select]
Option Explicit
Dim arrName1() As Variant
Dim arrName2() As Variant
Dim arrName3() As Variant
Dim blkName As String
Dim blkColor As Integer
Dim blkLayer As String
Dim blkLType As String
Const MyPath As String = "C:\"

Private Sub UserForm_Initialize()
Dim arrType1() As String
Dim arrType2() As String
Dim arrType3() As String
Dim arrTmp As Variant
Dim strTmp As String
Dim fFile As Integer
Dim I As Long

ReDim arrType1(0 To 999)
ReDim arrType2(0 To 999)
ReDim arrType3(0 To 999)

fFile = FreeFile

'**** Type 1
Open MyPath & "_Type1.txt" For Input As #fFile
'Need to skip the first 4 lines
For I = 0 To 4
    Line Input #fFile, strTmp
Next
I = 0
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType1(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType1(0 To I - 1)
ReDim arrName1(0 To I - 1, 0 To 4)
For I = 0 To UBound(arrType1)
    arrTmp = Split(arrType1(I), ",")
    arrName1(I, 0) = arrTmp(0)
    arrName1(I, 1) = arrTmp(1)
    arrName1(I, 2) = arrTmp(2)
    arrName1(I, 3) = arrTmp(3)
    arrName1(I, 4) = arrTmp(4)
Next
'End of Type 1

'**** Type 2
Open MyPath & "_Type2.txt" For Input As #fFile
'Need to skip the first 4 lines
For I = 0 To 4
    Line Input #fFile, strTmp
Next
I = 0
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType2(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType2(0 To I - 1)
ReDim arrName2(0 To I - 1, 0 To 4)
For I = 0 To UBound(arrType2)
    arrTmp = Split(arrType2(I), ",")
    arrName2(I, 0) = arrTmp(0)
    arrName2(I, 1) = arrTmp(1)
    arrName2(I, 2) = arrTmp(2)
    arrName2(I, 3) = arrTmp(3)
    arrName2(I, 4) = arrTmp(4)
Next
'End of Type 2

'**** Type 3
Open MyPath & "_Type3.txt" For Input As #fFile
'Need to skip the first 4 lines
For I = 0 To 4
    Line Input #fFile, strTmp
Next
I = 0
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType2(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType2(0 To I - 1)
ReDim arrName3(0 To I - 1, 0 To 4)
For I = 0 To UBound(arrType2)
    arrTmp = Split(arrType2(I), ",")
    arrName3(I, 0) = arrTmp(0)
    arrName3(I, 1) = arrTmp(1)
    arrName3(I, 2) = arrTmp(2)
    arrName3(I, 3) = arrTmp(3)
    arrName3(I, 4) = arrTmp(4)
Next
'End of Type 3
''Provide a default button, usually the first in the list
OptionButton1.Value = True
End Sub

Private Sub ListBox1_Click()
Dim I As Integer
I = ListBox1.ListIndex
blkName = ListBox1.List(I, 1)
blkLayer = ListBox1.List(I, 2)
blkColor = ListBox1.List(I, 3)
blkLType = ListBox1.List(I, 4)
End Sub


Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
    ListBox1.Clear
    ListBox1.List = arrName1
    ListBox1.ListIndex = 0
End If
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then
    ListBox1.Clear
    ListBox1.List = arrName2
    ListBox1.ListIndex = 0
End If
End Sub

Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then
    ListBox1.Clear
    ListBox1.List = arrName3
    ListBox1.ListIndex = 0
End If
End Sub

Private Sub CancelButton_Click()
Unload Me
End Sub

Sub InsertButton_Click()
Dim inspnt As Variant
Dim blkref As AcadBlockReference
Dim curLayer As String
curLayer = ThisDrawing.ActiveLayer.Name
Dim layer As AcadLayer
'''
On Error GoTo err_han
'''
    For Each layer In ThisDrawing.Layers
        If 0 = StrComp(layer.Name, blkLayer, vbTextCompare) Then
        ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
        Else: ThisDrawing.Layers.Add blkLayer
        ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
        End If
    Next layer
    For Each layer In ThisDrawing.Layers
        If layer.Name = blkLayer Then
        layer.color = blkColor
        layer.Linetype = blkLType
        End If
    Next layer
   
    If ThisDrawing.ActiveSpace = acModelSpace Then
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, blkName, 1, 1, 1, 0)
    End If
    If ThisDrawing.ActiveSpace = acPaperSpace Then
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, blkName, 1, 1, 1, 0)
    End If
   
ThisDrawing.ActiveLayer = ThisDrawing.Layers(curLayer)
ThisDrawing.Application.Update

Exit Sub

err_han:
Debug.Print Err.Number & Err.Description
   Exit Sub
End Sub

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #33 on: May 23, 2006, 02:19:09 PM »
oops... forgot the attachment.

Jeff_M

  • King Gator
  • Posts: 4042
  • C3D user & customizer
Re: Total newby to the swamp... VB block insertion
« Reply #34 on: May 23, 2006, 04:03:48 PM »
Remember that trick for getting the Immediate window open? CTRL + G......in case you forgot. Do that after it runs. You will see this:
? oview-2145320932AutoCAD main window is invisible

That is your error since you have the error reporting to Debug.Print  :-)

This means that you cannot select a point on screen when the dialog is up.....

Add this before selecting a point:
Me. Hide

Then after selecting the point:
Me.Show

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #35 on: May 23, 2006, 04:15:09 PM »
Jeff,

Thanks for the response. I figured I had to hide the form to do the actual picking. So, I did add the Me.Hide, not sure I need the Me.Show because all should be done....unless there is to be a multiple insert...which is another lesson.

Now I get this error...

-2145320939Invalid argument bstrName in InsertBlock

What is this type of error?....then lets see if I can figure out how to fix it.

Thanks.

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #36 on: May 23, 2006, 04:29:02 PM »
I'm not telling the app to get the blkName from the array am I?

Jeff_M

  • King Gator
  • Posts: 4042
  • C3D user & customizer
Re: Total newby to the swamp... VB block insertion
« Reply #37 on: May 23, 2006, 04:29:26 PM »
FWIW, I disagree with the way that Barry goes about his layer settings. There is no reason to set a layer current just so your Insert goes on it. Just change the Insert's layer property after inserting it, that way if there is an error you won't leave the user in a layer they don't expect.

This is more like how I think most would do this. Note that your code runs through the layer collection twice each insertion! This never loops through it, but accesses the layer direct, or adds it if an error is thrown due to it not existing.

As I was posting this I saw that you added a new question....I don't get any errors in the following code. Maybe it's erroring because of not using the Me.Show to allow the user to exit the program. By just leaving at the Me.Hide you are leaving a running instance of your program...... if you wish to exit after inserting 1 block, add Unload Me to the end of the Insert button code, in place of the Me.Show.

Code: [Select]
Sub InsertButton_Click()
Dim inspnt As Variant
Dim blkref As AcadBlockReference
Dim layer As AcadLayer
'''
'''
    On Error Resume Next
    Set layer = ThisDrawing.Layers.Item(blkLayer)
    If Err.Number <> 0 Then
        Err.Clear
        Set layer = ThisDrawing.Layers.Item(blkLayer)
        layer.color = blkColor
        layer.Linetype = blkLType
    End If
   
    On Error GoTo err_hand
    '''you should have something here to verify the block can be loaded.
    Me.hide
    If ThisDrawing.ActiveSpace = acModelSpace Then
        inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
        Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, blkName, 1, 1, 1, 0)
        blkref.layer = blkLayer
    End If
    If ThisDrawing.ActiveSpace = acPaperSpace Then
        inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
        Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, blkName, 1, 1, 1, 0)
        blkref.layer = blkLayer
    End If
ThisDrawing.Application.Update
    Me.Show

Exit Sub

err_han:
Debug.Print Err.Number & Err.Description
   Exit Sub
End Sub

Jeff_M

  • King Gator
  • Posts: 4042
  • C3D user & customizer
Re: Total newby to the swamp... VB block insertion
« Reply #38 on: May 23, 2006, 04:32:09 PM »
I'm not telling the app to get the blkName from the array am I?
Yes, you are. That happens whenever you select the block name:
Look at the code for the ListBox1_Click event

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #39 on: May 23, 2006, 04:33:58 PM »
so blkref.layer changes the reference of all blocks of that name to blkLayer.... or just that instance of the block?

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #40 on: May 23, 2006, 04:35:09 PM »
ListBox1....blkName....duh... I see that now

Bob Wahr

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #41 on: May 23, 2006, 04:39:22 PM »
A block reference is one insertion of a block so you are changing the one instance only.

Bob Wahr

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #42 on: May 23, 2006, 04:42:03 PM »
When you are testing your code, do you just run the code and see what the end result is or do you step through your code?  stepping through your code (F8) helps a lot in seeing what is happening and will really help you to see what's going on.

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #43 on: May 23, 2006, 04:43:37 PM »
I have just been running it. So... now.... I will try your suggestion.

Thank you.

Jeff_M

  • King Gator
  • Posts: 4042
  • C3D user & customizer
Re: Total newby to the swamp... VB block insertion
« Reply #44 on: May 23, 2006, 04:49:18 PM »
Not to change the subject, but.... Hi Bob! Good to see you back!