TheSwamp

Code Red => VB(A) => Topic started by: akdrafter on December 15, 2005, 02:25:43 PM

Title: Total newby to the swamp... VB block insertion
Post by: akdrafter on December 15, 2005, 02:25:43 PM
Hello All,

In a nut shell.... I would like to create a VB app that will insert a block on a layer according to the block being inserted, scaled to the current dimscale but if in pspace dimscale set to 1. I am not asking for the full blown app, but a primer would be helpful....or hints....or examples....or etc....

I am totally new to VB. Can't seem to get the $$ peeps to open the wallet for formal training and trying to hack things out on company time yields some funky eyebrow movements.

Thanks in advance.

Akdrafter
Title: Re: Total newby to the swamp... VB block insertion
Post by: deegeecees on December 15, 2005, 03:25:04 PM
I feel yer pain ak. Just to let you know there's somebody here to help. I'm extremely busy at the moment, so if noone else comes along, I'll give you a hand when I can. This will give this thread a little bump anyway.

 :-)
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on December 15, 2005, 03:52:53 PM
Deegeecees,

Thanks for the reply. I have been exploring the VB editor and trying to lay out how the app would look and feel. Also writing down different scenarios and possible problems, etc etc...

Kerry Brown in the former CadVault helped me a great deal in creating a lisp routine that set the layer, scale, etc parameters from the coding in the image tile menu and used those parameters for the block insertion. My fear is the image tile menu will be disappearing here soon and I am not fond of the Design center or tool palettes. For what we do they seem to create more work than is necessary. That said I am more interested in customized lisp and VB apps for image tile menu type stuff and other company customizations.

Thanks in advance and I am in no rush.

Akdrafter
Title: Re: Total newby to the swamp... VB block insertion
Post by: deegeecees on December 15, 2005, 04:01:48 PM
Yep, Design Center is more of a generalized solution to a multi-faceted issue. It's more productive to have a tool that does EXACTLY what you want rather than just some. But, I have about 250 dwgs to go through in front of me, so if you're still foraging, try the Links page here at the Swamp. AfraLisp may have a good start for you. Good tutorials, great Owner/Creator.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Barry Clark on December 15, 2005, 04:52:46 PM
Are you looking for code or just a walk through of the method?

I have tons of these routines. Some of them, though, are very old and have many redundancies.

Code: [Select]
Sub BlockIns()
Dim inspnt As Variant
Dim blkref As AcadBlockReference
Dim LayerCurrent As String
LayerCurrent = ThisDrawing.ActiveLayer.Name
Dim layer As AcadLayer
Dim layername As String
On Error GoTo err_han
layername = "layername"
    For Each layer In ThisDrawing.layers
        If 0 = StrComp(layer.Name, layername, vbTextCompare) Then
        ThisDrawing.ActiveLayer = ThisDrawing.layers("layername")
        Else: ThisDrawing.layers.Add ("layername")
        ThisDrawing.ActiveLayer = ThisDrawing.layers("layername")
        End If
    Next layer
    For Each layer In ThisDrawing.layers
        If layer.Name = "layername" Then
        layer.color = [i]choose your color, here[/i[
        layer.Linetype = "[i]put your linetype here[/i]"
        End If
    Next layer
    If ThisDrawing.ActiveSpace = acModelSpace Then
    inspnt = ThisDrawing.Utility.GetPoint(, vbCr & "Pick Insertion Point: ")
    Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, "path to dwg", 1, 1, 1, 0)
    End If
    If ThisDrawing.ActiveSpace = acPaperSpace Then
    inspnt = ThisDrawing.Utility.GetPoint(, vbCr & "Pick Insertion Point: ")
    Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, "path to dwg", 1, 1, 1, 0)
    End If
ThisDrawing.ActiveLayer = ThisDrawing.layers(LayerCurrent)
ThisDrawing.Application.Update
Exit Sub

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


There is likely a better way or, at very least, cleaner way but this works for me.

I removed the items that are very specific to what I am doing and replaced them with something descriptive as to what to do in that spot. Sorry if I left something confusing in there.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on December 15, 2005, 06:38:42 PM
Here is my thought process....lengthy and I apologize. I am just trying to explain my thought process. I don't want to have the app done for me....Ideas and code snippets to help me compile the whole thing. Again, I am hacking my way through this and I sincerely apologize to the code gurus.

One step at a time... So here we go.

We have symbols(blocks) we use on our drawings. There are various things that happen to those blocks when they are inserted into a drawing. I.e.

Insert point
Layer
Insertion Scale: (if space = model  set insert scale to current value of dimscale     if space = paper  set insert scale to 1.0)
Rotation
Mirror
Attributes (show dialoge to enter attribute information)
Explode

Of course there is an error checking and restoration funciton.

So....

If:

Block1: do 1 2 3 4 5
Block2: do 2 5 4
Block3: do 1 4 5
...
Block88: do 1 2 3 5
etc....

I have attached an image of the basic idea for a layout of the VB app.

It would be nice to see an slide type view of the block to be inserted. I am also planning on using a "description" of the block in the "Block Description" area instead of the actual block name.

I may have to create some sort of "INI" style file to group the different types of descriptions. Not sure on this one. The idea is if you click on "Wood Columns" only wood column blocks would appear in the "Block Description" area.

The "Existing Item?" check box would make the block insert on our "EXST" layers. The "Current Dimscale" would just be a warm fuzzy "What are my current settings" type quck reference. There are others I would like to add to it, but I will learn that later.

I have attached an image of the app layout(work in progress).

Here is the code and that Kerry Brown basically wrote to set the insertion parameters and a sample menu lines that uses the routine.

Menu Line:
[BLOCK1]^C^C^P(BLKINS "LAYER1" "1" "CONTINUOUS" "BLOCK1" 1.0 0.0 nil nil nil)

Image Tile Menu Line:
[SLIDE-IMAGES(BLOCK1,BLOCK1 DESCRIPTION)]^C^C^P^P(BLKINS "LAYER1" "1" "CONTINUOUS" "BLOCK1" 1.0 0.0 nil nil nil)

Quote
(defun BLKINS (LA_NAM LA_COL LA_LT BLKNAM INSSCL ROT MIR ATTS EXP / INSPT)
(INITERR)
(setvar "cmdecho" 0)
(vl-load-com)
(command ".undo" "end")
(command ".undo" "m")
(setvar "cecolor" "bylayer")
(setvar "celtype" "bylayer")
(setvar "celweight" -1)
(setvar "snapmode" 0)
(setvar "attreq" 0)
;;;
(or INSSCL (setq INSSCL (getvar "dimscale")))
;;;
(setq INSPT (getpoint "\nPick Insertion Point: "))
;;;
(vl-cmdf ".-layer" "make" LA_NAM "thaw" LA_NAM "on" LA_NAM "color" LA_COL LA_NAM "lt" LA_LT LA_NAM "")
;;;
(if ROT
(vl-cmdf ".-insert" BLKNAM INSPT INSSCL INSSCL ROT)
   (progn
   (princ "\nRotation Angle: ")
   (vl-cmdf ".-insert" BLKNAM INSPT INSSCL INSSCL pause)
   )
)
;;;
(if MIR
   (vl-cmdf ".mirror" (entlast) "" INSPT pause "y")
)
;;;
(if ATTS
(vl-cmdf ".ddatte" (entlast))
)
;;;
(if EXP
(vl-cmdf ".explode" (entlast))
)
;;;
(command ".undo" "end")
(RESET)
(princ)
)

Thanks
Title: Re: Total newby to the swamp... VB block insertion
Post by: Arizona on December 16, 2005, 06:17:48 AM
Akdrafter,

That is very ambitious for a first time app. The code that Barry posted contains much of what you actually need for the basic function. I would suggest (since you are learning as you go) that you break down your code into little pieces (tasks). In other words, the code Barry supplied shows you how to get and store current layer, which space, insertion point, etc... work with getting this piece to work for you then add some of your additional settings/variables. Work from the inside out (so to speak), get the easier inside stuff working before you work on the container. I would recommend that you use a flowchart to figure out where you want to go.
Also, are you trying to write a VBA or VB app for AutoCAD? The difference is whether you are writing the app using the VBA editor inside of Acad or you using a stand-alone VB editor.
And what version of Acad are you referencing?
Title: Re: Total newby to the swamp... VB block insertion
Post by: hendie on December 16, 2005, 06:33:45 AM
also to note: if you are using any form of VB, you will not be able to view any slide files, although you can preview the drawing bitmap
I agree with Arizona ~ a flowchart would be a good start along with some pseudo code. I see Kerry's code is Lisp and you have mentioned using VB ~ if I were you  I would try and avoid using both languages, especially in a first app.. it can get messy.
You could separate your blocks out by either using (reading) a text file list or by using separate directories for different block styles.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Kerry on December 16, 2005, 06:48:30 AM
Is it Alan ?

My memory must be shot,  'cause I cant remember that code :-)

I may have helped with bits, but you're being very generous to say I  "basically wrote it".

Good to see you here !
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on December 16, 2005, 01:08:39 PM
Hey Tim! Glad to see you found us here at theSwamp.
If you get stuck, don't hesitate to ask. As you can see, there are plenty of helping hands around here.

Jeff

Kerry, I recall the thread that code was in...over at CV about 18 months ago......
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on December 16, 2005, 03:42:51 PM
Arizona,

Trying to write an app for AutoCAD using the VB editor in AutoCAD. Version 2006 and as soon as 2007 comes out it will be 2007. Shall cross that bridge when I come to it though.  :-D

Ok, lets start by first aquiring the list of files to use in the Block Description listbox. I have thought of creating separate directories for the different types of blocks, but that gets "Path" messy. Can I create some sort of "List" to read for each of the "Block Types" ? In other words, leave all of the blocks in one directory but have a list for each of the "Block Types".

Kerry,

The blkins code you wrote/helped me with/created/painted/coded/dreamed up/pulled out of...errr...hahahaha....you get the point. Simply said...it works and works very well. It sure does create consistancy from the staff. Point and shoot. Yeah baby. Anyway. Thanks and I still owe ya that beer if ya ever come up here to AK.
Title: Re: Total newby to the swamp... VB block insertion..hacking away....
Post by: akdrafter on December 19, 2005, 05:15:42 PM
Hello,

Ok. Here is a start. I have been reading the AfraLisp web site quite heavily.http://www.afralisp.co.za/ I have taken some code from the http://www.afralisp.co.za/vba/extext.htm section of the web site. So.....

My first "Event" is reading the content of a text file into the listbox according to which radio button is selected by the user. If radio button "Type A" is selected, then "_TypeA.txt" will be read into the list box. The formatting of "_TypeA.txt" is as follows:

*Description of Block to be Inserted
Actual Block to be Inserted

The underscore in the text file name is just so the file appears at the top of the file list (can be changed later). I have attached the "_TypeA.txt" text file.

I have tried to get the insertion part of the app to work, but with no luck. I am certain it has to do with the way I try to feed the variable name of the block to be inserted. Clueless there. work in progress....

So, my first event is the reading of the appropriate text file into the listbox based on which radio button is selected. Hints...tips....ideas?

Also, can anyone tell me how I would display the current dimscale variable in a text box...or whatever type of box is used to display something like that?

Code: [Select]
'CODING STARTS HERE

' Credit To The Following:
' Kenny Ramage of the AfraLisp Website http://www.afralisp.co.za/
' Barry Clark from http://www.theswamp.org

Option Explicit

Private Sub TypeButtonA_Click()

End Sub

Private Sub TypeButtonB_Click()

End Sub

Private Sub UserForm_Initialize()

Dim sTempa As String
Dim nFilea As Integer

On Error GoTo err_handler

'setup and format the form
UserForm1.Caption = "Xyz Company Block Insertion"
InsertButton.Caption = "Insert Block"
InsertButton.Accelerator = "I"
InsertButton.Default = False
CancelButton.Caption = "Cancel"
CancelButton.Accelerator = "C"
CancelButton.Cancel = False
TypeButtonA.Caption = "Block Type A"
TypeButtonB.Caption = "Block Type B"
Label1.Caption = "Select Block :"

'=============================================
'=============================================

'get the next free file number
nFilea = FreeFile

'clear listbox
ListBox1.Clear

'open the text file
Open "C:\_TypeA.txt" For Input As #nFilea

'until the end of file
While Not EOF(nFilea)

'read the line and store it in a variable
Line Input #nFilea, sTempa

'isolate the first character and test
If Left$(sTempa, 1) = "*" Then

'remove the first character
sTempa = Right$(sTempa, (Len(sTempa) - 1))

'display the remainder in the list box
ListBox1.AddItem sTempa

'end if
End If

'loop
Wend

'close the file
Close #nFilea

Exit Sub

err_handler:

MsgBox "Error No " & Err.Number & " - " & Err.Description
Err.Clear
Exit Sub

End Sub

Private Sub InsertButton_Click()

Dim sTemp As String
Dim sTemp1 As String
Dim nFile As Integer
Dim inspnt As Variant
Dim blkref As AcadBlockReference
Dim sBlkNam As String

On Error GoTo err_handler

'hide the form
UserForm1.Hide

'get the selection from the list box
sTemp = ListBox1.Text

'get the next free file number
nFile = FreeFile

'open the text file
Open "C:\_TypeA.txt" For Input As #nFile

'until the end of file
While Not EOF(nFile)

'read the line and store it in a variable
Line Input #nFile, sTemp1

'compare the selection
If sTemp1 = "*" & sTemp Then

'read the next line and retrieve the values from the text file
'storing them into their respective variables
Input #nFile, sBlkNam

'close the file
Close #nFile

'you have now retrieved all the values necessary to draw
'the un-equal angle.
'coding for that would go here.

    If ThisDrawing.ActiveSpace = acModelSpace Then
    inspnt = ThisDrawing.Utility.GetPoint(, vbCr & "Pick Insertion Point: ")
    Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, sBlkNam, 1, 1, 1, 0)
    End If
    If ThisDrawing.ActiveSpace = acPaperSpace Then
    inspnt = ThisDrawing.Utility.GetPoint(, vbCr & "Pick Insertion Point: ")
    Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, sBlkNam, 1, 1, 1, 0)
    End If

'we're done, so exit
Exit Sub

'end if
End If

'loop
Wend

'close the file
Close #nFile

Exit Sub

err_handler:

MsgBox "Error No " & Err.Number & " - " & Err.Description
Err.Clear
Exit Sub

End Sub


Private Sub CancelButton_Click()
End
End Sub

Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on December 19, 2005, 06:12:01 PM
Current Dimscale via "Label"

Label2.Caption = "Current DimScale: " + Format$(ThisDrawing.GetVariable("DIMSCALE"))

Wowzers batman. I actually did something on my own. Sheesh. That only took a week. hahahahahahaha

If you can't laugh at yourself.....
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on December 20, 2005, 05:23:36 PM
I am making progress.

To change the contents of the list box this works....

Code: [Select]
Private Sub OptionButtonA_Click()

 Dim sTemp As String
 
 'clear listbox
ListBox1.Clear

'get the next free file number
nFilea = FreeFile

'clear listbox
ListBox1.Clear

'open the text file
Open "C:\_TypeA.txt" For Input As #nFilea

'until the end of file
While Not EOF(nFilea)

'read the line and store it in a variable
Line Input #nFilea, sTempa

'isolate the first character and test
If Left$(sTempa, 1) = "*" Then

'remove the first character
sTempa = Right$(sTempa, (Len(sTempa) - 1))

'display the remainder in the list box
ListBox1.AddItem sTempa

'end if
End If

'loop
Wend

'close the file
Close #nFilea

End Sub

Yes, I am changing the naming of the buttons and such as I go along. I guess that is my way of becoming more aware of what things like DIM mean.

I still do not know why I have to hard code the path to the text files. Anyone know why?

Comments? Suggestions?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on December 20, 2005, 06:34:32 PM
Tim, Not a lot of time here as I must get ready to drive up to Oregon. But the biggest thing I notice is the number of times you read a file. IMHO, you should read all of your files at the time of starting the form (Initialize Event) and store the Data in either arrays or collections to minimize the opening/closing of files. If you use an array, you can then fill your listbox from the array and also reference back to that, or another array that has the actual block names, from the listindex.
Code: [Select]
Option Explicit
Dim arrName1() As String
Const MyPath As String = "C:\Base Maps\Jeffs Test Files\junk\Tim\"

Private Sub UserForm_Initialize()
Dim arrType1() As String
Dim strTmp As String
Dim fFile As Integer
Dim I As Long

ReDim arrType1(0 To 999)
ReDim arrName1(0 To 999)

fFile = FreeFile
'**** Repeat the following for each type of block you have
Open MyPath & "_TypeA.txt" For Input As #fFile
'Need to skip the first 4 lines
For I = 0 To 3
    Line Input #fFile, strTmp
Next
I = 0
Line Input #fFile, strTmp
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType1(I) = Replace(strTmp, "*", "")
    Line Input #fFile, strTmp
    arrName1(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType1(0 To I - 1)
ReDim Preserve arrName1(0 To I - 1)
ListBox1.List = arrType1
''Stop the repeat here

End Sub
Now, since both arrays are matched index for index, you can access the second array "arrName1" when the Insert button is selected like so:
Code: [Select]
blkName = arrName1(ListBox1.ListIndex)
You could do the same thing with each of your block Types.....just add a arrNameX() to the Public declarations and a arrTypeX() to the local declarations for each X, then add the appropriate code.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on December 22, 2005, 05:49:42 PM
In my haste to get out of town I forgot to add that I would use 1 listbox for each of the different sets of blocks you have. Place them on top of each other and make only 1 visible at a time. So in the Click Event of each Optionbutton it would have something like this:
Code: [Select]
Sub SheetOption_Click ()
If SheetOption.Value = True Then
  SheetListbox.Visble = True
  'All other Listboxes must be set to False
Else
  SheetListbox.Visible = False
End If
End Sub

And I do hope I didn't scare you away......  8-) Have a Merry Christmas!
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on December 22, 2005, 06:00:25 PM
Jeff,

No, you have not scared me away. I actually found a book on VB6 and I am begining to understand some of the lingo I have heard. I am trying to understand and my attention to any one thing is diminished when I have to pull out the fire hose to put out the latest fire around here.

I will be off and on with this for a while, but yet again... I am in no hurry and I think it is more important I make a good effort to understand what it is that I am doing rather than cut and paste someones code. Your help is "Greatly" appreciated and I must say a refreshing escape from the norm on another forum...** Clears Throat **.

Thank you Jeff and others and have a Merry Christmas/Holiday.
Title: VB block insertion revisited...
Post by: akdrafter on May 18, 2006, 02:49:07 PM
Hello,

I am revisiting this thread because I basically dropped it and I am now trying to pick it back up.

Jeff.... I have the following code from your example of reading the contents of the text file at Initialize time and your advise about making only the current list box visible is a good one but I can't seem to get it to work.

Suggestions?

I have attached the dvb and the 3 text files.

AKDrafter


Code: [Select]
Option Explicit
Dim arrName1() As String
Dim arrName2() As String
Dim arrName3() As String
Const MyPath As String = "C:\"


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

ReDim arrType1(0 To 999)
ReDim arrName1(0 To 999)
ReDim arrType2(0 To 999)
ReDim arrName2(0 To 999)
ReDim arrType3(0 To 999)
ReDim arrName3(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 3
    Line Input #fFile, strTmp
Next
I = 0
Line Input #fFile, strTmp
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType1(I) = Replace(strTmp, "*", "")
    Line Input #fFile, strTmp
    arrName1(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType1(0 To I - 1)
ReDim Preserve arrName1(0 To I - 1)
ListBox1.List = arrType1
'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 3
    Line Input #fFile, strTmp
Next
I = 0
Line Input #fFile, strTmp
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType2(I) = Replace(strTmp, "*", "")
    Line Input #fFile, strTmp
    arrName2(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType2(0 To I - 1)
ReDim Preserve arrName2(0 To I - 1)
ListBox2.List = arrType2
'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 3
    Line Input #fFile, strTmp
Next
I = 0
Line Input #fFile, strTmp
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType3(I) = Replace(strTmp, "*", "")
    Line Input #fFile, strTmp
    arrName3(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType3(0 To I - 1)
ReDim Preserve arrName3(0 To I - 1)
ListBox3.List = arrType3
'End of Type 3

End Sub

Private Sub CancelButton_Click()
End
End Sub

Sub OptionButton1_Click()
If OptionButton1.Value = True Then
    ListBox1.Visible = True
Else
    ListBox1.Visible = False
End If
End Sub

Sub OptionButton2_Click()
If OptionButton2.Value = True Then
    ListBox2.Visible = False
Else
    ListBox2.Visible = True
End If
End Sub

Sub OptionButton3_Click()
If OptionButton3.Value = True Then
    ListBox3.Visible = False
Else
    ListBox3.Visible = True
End If
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 18, 2006, 03:03:45 PM
 :-D Yes... the list box visible settings are tweaked a bit...hahahahaah

I think it should be something like this.

Code: [Select]
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
    ListBox1.Visible = True
Else
    ListBox1.Visible = False
End If
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then
    ListBox2.Visible = True
Else
    ListBox2.Visible = False
End If
End Sub

Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then
    ListBox3.Visible = True
Else
    ListBox3.Visible = False
End If
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 18, 2006, 03:27:34 PM
Ok. Solving my own problems...hahahahaah

This works, but I am not sure if it is the most effective or correct way to hide/make visible each list box based on which OptionButton is picked.

Code: [Select]
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
    ListBox1.Visible = True
    ListBox2.Visible = False
    ListBox3.Visible = False
End If
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then
    ListBox2.Visible = True
    ListBox1.Visible = False
    ListBox3.Visible = False
End If
End Sub

Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then
    ListBox3.Visible = True
    ListBox1.Visible = False
    ListBox2.Visible = False
End If
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 18, 2006, 03:47:04 PM
Keep talkin' Tim and all your problems will be solved :-)

I'm gonna have a look-see right now.

Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 18, 2006, 03:50:49 PM
Thanks Jeff. I am moving on to the insertion part now.

Fingers crossed.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 18, 2006, 04:15:21 PM
OK, back to the form.....first, to make typing easier (i.e.-use copy-paste) I usually keep all of my Optionbutton's code in the same order. That way I set up the first one, then copy-paste it however many times I have additional buttons, then just change the number and the actions. Not that big a deal, but I really don't like to type more than necessary.....

Second, I'm attaching the form & code how I'd do it. Note that I stacked all 3 listboxes on top of each other..........

I also added default selected items so if the user presses insert before they actually select something, at least something will be done...an error would be thrown otherwise.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 18, 2006, 04:44:48 PM
Jeff,

I see how you set the "properties" using code instead of setting the the property via the properties. If that makes sense. hahahaha

I had the list boxes and their option buttons spread out so that I could see the on/off of the list boxes. I have since lined things up and such.

Now concerning the insertion part of the routine. Is this how I would store the value of the block to be inserted?

blkName = arrName3(ListBox3.ListIndex)

Code: [Select]
'**** Type 3
Open MyPath & "_Type3.txt" For Input As #fFile
'Need to skip the first 4 lines
For I = 0 To 3
    Line Input #fFile, strTmp
Next
I = 0
Line Input #fFile, strTmp
While strTmp <> "" 'this will stop it once it encounters a blank line
    arrType3(I) = Replace(strTmp, "*", "")
    Line Input #fFile, strTmp
    arrName3(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
Wend
Close #fFile
ReDim Preserve arrType3(0 To I - 1)
ReDim Preserve arrName3(0 To I - 1)
ListBox3.List = arrType3
blkName = arrName3(ListBox3.ListIndex)
'End of Type 3

Would I add that to each type and then I would have the "blkName" to use as the variable in an insertion function?

Also, I am thinking of adding a line to the "Type" text file after or before the "actual" block name to be inserted that would have layer properties (name,color & linetype) that I could also feed to the insertion function.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 18, 2006, 05:07:53 PM
This is how I would use the blkName variable.....first I'd declare it as a Global, then at the end iof the initialize event:
Code: [Select]
ListBox3.List = arrType3
'End of Type 3
''Provide a default button, usually the first in the list
OptionButton1.Value = True
ListBox1.Visible = True
ListBox1.Selected(0) = True
blkName = arrname(ListBox1.ListIndex)
ListBox2.Visible = False
ListBox2.Selected(0) = True
ListBox3.Visible = False
ListBox3.Selected(0) = True
End Sub
Then for each ListBoxX_Click event:
Code: [Select]
Private Sub ListBox1_Click()
blkName = arrName1(ListBox1.ListIndex)
End Sub
Private Sub ListBox2_Click()
blkName = arrName2(ListBox2.ListIndex)
End Sub
Private Sub ListBox3_Click()
blkName = arrName3(ListBox3.ListIndex)
End Sub

Adding the property information cold be done fairly easily, too. Will all of the Type1 blocks go on the same layer with the same color & linetype, or will each one be different?

Ya know, I sometimes wonder at how I arrive at some of my decisions.....thinking back on the Listboxes.......crud, I should've thought this way originally. I'll post something new for you to chew on in a few.....

You will probably want to hunt me down to bop me upside the head, but......
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 18, 2006, 05:59:19 PM
To answer your question about the layering for the different types...

Some....hahahaha

Type1 = Layer1
Type2 = Layer2
Type3 = Layer3
Type4 = Various Layers

So, that is why my thought is to add the layer properties in the text file next to the actual block name to be inserted. There would be other items that could be useful. Like rotation, scale based on paper or model space, mirrioring, etc. So, my thought was that if I knew how to set the layer props in the text file, I would understand how to set the other items within the text file as well. I used the code from Kerry to have the various props set via the image tile menu before the insertion routine begins. So, it was definately much easier to set the various props of the different kinds of blocks to be inserted.

The insertion code Kerry Brown wrote:

Code: [Select]
(defun BLKINS (LA_NAM LA_COL LA_LT BLKNAM INSSCL ROT MIR ATTS EXP / INSPT)
(INITERR)
(setvar "cmdecho" 0)
(vl-load-com)
(command ".undo" "end")
(command ".undo" "m")
(setvar "cecolor" "bylayer")
(setvar "celtype" "bylayer")
(setvar "celweight" -1)
(setvar "snapmode" 0)
(setvar "attreq" 0)
;;;
(or INSSCL (setq INSSCL (getvar "dimscale")))
;;;
(setq INSPT (getpoint "\nPick Insertion Point: "))
;;;
(vl-cmdf ".-layer" "make" LA_NAM "thaw" LA_NAM "on" LA_NAM "color" LA_COL LA_NAM "lt" LA_LT LA_NAM "")
;;;
(if ROT
(vl-cmdf ".-insert" BLKNAM INSPT INSSCL INSSCL ROT)
   (progn
   (princ "\nRotation Angle: ")
   (vl-cmdf ".-insert" BLKNAM INSPT INSSCL INSSCL pause)
   )
)
;;;
(if MIR
   (vl-cmdf ".mirror" (entlast) "" INSPT pause "y")
)
;;;
(if ATTS
(vl-cmdf ".ddatte" (entlast))
)
;;;
(if EXP
(vl-cmdf ".explode" (entlast))
)
;;;
(command ".undo" "end")
(RESET)
(princ)
)
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 18, 2006, 07:30:38 PM
OK, here's a different spin on the Listboxes and Text files. It utilizes just one listbox, no matter how many different block TYPES you want to include. It does require a separate, global, Array for each Type. ALso, since each row from the text file is converted to and array of rows & columns, when you add more "columns" to the text file you will need to make sure to update that number in the ReDim of the arrays. Take a look and see how you will be able to grab all of the data you need for each insertion quite easily. This still uses your same form, but I removed the ListBoxes 2 & 3. Make sure to modify your Text files as I noted in the code.
Code: [Select]
'This version uses only 1 listbox......and changes how the TEXT file is arranged _
  which will allow for easy expansion for the scale, rotation, etc.
'Typical text file:

'--------------------
'Type 1
'--------------------
'Description,BlockName,Layer,Color,Linetype
'Type1-1,Block1,Layer1,2,CONTINUOUS
'Type1-2,Block2,Layer1,2,CONTINUOUS
'Type1-3,Block3,Layer1,2,CONTINUOUS
'Type1-4,Block4,Layer1,2,CONTINUOUS
'Type1-5,Block5,Layer1,2,CONTINUOUS
'
'--------------------
'Type 1
'--------------------

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 CommandButton1_Click()
Debug.Print blkName & " " & blkLayer
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 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 CancelButton_Click()
Unload Me
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
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 18, 2006, 08:04:21 PM
How do I get that Debug.Print to work? I am pretty sure a person uses it to see what the value of the dim's are.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bryco on May 18, 2006, 08:07:27 PM
akdrafter, a menu is also a very good way of inserting your blocks.
I think you mentioned columns, if you have say 10 columns and they can easily be described by a submenu heading like 10" dia., 8" dia. then it is quicker to grab them from a pull down menu than wait for the gui to load (not much, perhaps I should say less noisey). If there  is no need for a picture of the block then you dont put it in. You can also load a gui from the menu for the items that just suit that format better.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bryco on May 18, 2006, 08:08:43 PM
view-> immediate window
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 18, 2006, 08:15:04 PM
view-> immediate window
or CTRL + G
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter 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
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter 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
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 02:19:09 PM
oops... forgot the attachment.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M 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
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter 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.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 04:29:02 PM
I'm not telling the app to get the blkName from the array am I?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M 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
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M 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
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter 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?
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 04:35:09 PM
ListBox1....blkName....duh... I see that now
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr 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.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr 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.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 04:43:37 PM
I have just been running it. So... now.... I will try your suggestion.

Thank you.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 04:49:18 PM
Not to change the subject, but.... Hi Bob! Good to see you back!
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 04:59:54 PM
You will also want to familiarize yourself with Step Over (Shift+F8) and Step Out (Ctrl+Shift+F8) which will help you get through loops much less painlessly.  Another handy thing is setting breakpoints, which you do by clicking the bar on the left side of the code pane.  This puts a dot next to the line.  You can then run the program (F5) and it will stop when it comes to that line at which point you can start stepping through the code.  I use this a lot when I get to new bits of a routine, I know the first part is running as desired so I let it go and just start stepping at the part I am working on.

I would also advise you to keep your locals window open.  It takes up real estate but is a good way to check values while you are stepping through the code.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 05:01:39 PM
Good to be back at least part time Jeff.  It's been a pretty rough year but hopefully is at least levelling out.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 05:07:15 PM
My blkref is coming up empty.

-2145386445Filer error

My apologies for being a pain.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 05:09:46 PM
no problem at all, ACAD's not finding the block.  Try this
Code: [Select]
    If ThisDrawing.ActiveSpace = acModelSpace Then
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 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, MyPath & blkName & ".dwg", 1, 1, 1, 0)
    End If
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 05:13:31 PM
For that matter, if active space is NOT ModelSpace, then it IS PaperSpace, there's no middle ground so instead of two If statements, you can combine them into one so you can go with
Code: [Select]
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    If ThisDrawing.ActiveSpace = acModelSpace Then
      Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
    Else
      Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
    End If
Which is easier to follow for me at least.  If it's easier for you to keep track of it as two separate If statements, it's really not going to be too much of a drag so I would say go with what you can follow easiest (at least for now).
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 05:17:58 PM
to add that togather with Jeff's layer suggestion on the layering
Code: [Select]
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    If ThisDrawing.ActiveSpace = acModelSpace Then
      Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
    Else
      Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
    End If
    blkref.Layer = blkLayer
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 05:19:40 PM
I figured there was a pathing issue. But, I was also wondering if I had the "Support File Search Path" set in my preferences.... would the app find the block if it was in one of those paths?

Could I create a variable that contained the blkName & ".dwg" extension so that I would have to have that in the InsertBlock. I would have figured that it would know it is to be a .dwg
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 05:25:56 PM
For that matter, if active space is NOT ModelSpace, then it IS PaperSpace...
While technically true, and I was going to advise Tim on this once he got everything thing else working, if the user is in ModelSPace inside of a PaperSpaceViewport Active space returns Paperspace, which is not where you want the block to be placed.....

Which is why I use:
Code: [Select]
Dim Space as AcadBlock
If ThisDrawing.GetVariable("CVPORT") = 1 Then
   Set Space = ThisDrawing.PaperSpace
Else
   Set Space = ThisDrawing.ModelSpace
End If

Set blkins = Space.InsertBlock.........
Which insures I will always work in the space that the user is working in.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 05:28:23 PM
Yes... CVPORT

I was reading up on that in another thread. Was going to cross that bridge soon. :-)
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 05:28:53 PM
.... would the app find the block if it was in one of those paths? No

Could I create a variable that contained the blkName & ".dwg" extension so that I would have to have that in the InsertBlock. I would have figured that it would know it is to be a .dwg
Yes, and I'd go further and say that you should check if it is already in the drawing and only add the path & ".dwg" if it isn't, otherwise it will be read from disk everytime.....
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 05:31:07 PM
you can, when you get to insertion time change blkname by doing a

blkname = mypath & blkname & ".dwg"

then for the insertion, you can just use blkname, you could also add a new variable called insname or whatever else you want.

It doesn't know that it's a drawing because the InsertBlock method, inserts a block that is defined in the drawing, if there's not a block already defined in the drawing, you need to get explicit.  

*Good point Jeff, I was trying to simplify the statement and hadn't considered that.

**grr you guys are both typing faster than me, not fair.  Jeff, I was leaving the check for definition bit for later.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 05:38:33 PM
**grr you guys are both typing faster than me, not fair.  Jeff, I was leaving the check for definition bit for later.
Heh, that's the first time I've ever been accused of *THAT* :lmao: I'm leaving for a bit anyway......I'll butt out for a while. ;-)
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 06:16:04 PM
Just when I thought it was working I am getting the following error....and the layer is not being created nor is the block being inserted on that layer.

-2145386476Key not found

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_han
    '''you should have something here to verify the block can be loaded.
    Me.hide
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    If ThisDrawing.ActiveSpace = acModelSpace Then
      Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkref.layer = blkLayer
    Else
      Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkref.layer = blkLayer
    End If
ThisDrawing.Application.Update
Unload Me

Exit Sub

err_han:
Debug.Print Err.Number & Err.Description
   Exit Sub
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 23, 2006, 06:23:45 PM
W/O scrolling up to your other code, did you ever use the ADD method to create it if it didn't exist?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 06:28:41 PM
Never Dim something with the same name as a property, method, etc.
Code: [Select]
Sub InsertButton_Click()
Dim inspnt As Variant
Dim blkref As AcadBlockReference
Dim objLayer As AcadLayer
'''
'''
    On Error Resume Next
    Set objlayer = ThisDrawing.Layers.Item(blkLayer)
    If Err.Number <> 0 Then
        Err.Clear
        Set objlayer = ThisDrawing.Layers.Item(blkLayer)
        objlayer.color = blkColor
        objlayer.Linetype = blkLType
    End If
   
    On Error GoTo err_han
    '''you should have something here to verify the block can be loaded.
    Me.hide
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    If ThisDrawing.ActiveSpace = acModelSpace Then
      Set blkref = ThisDrawing.ModelSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkref.layer = blkLayer
    Else
      Set blkref = ThisDrawing.PaperSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkref.layer = blkLayer
    End If
ThisDrawing.Application.Update
Unload Me

Exit Sub

err_han:
Debug.Print Err.Number & Err.Description
   Exit Sub
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 06:31:45 PM
The tipoff here is that in blkref.Layer = blkLayer, Layer should be capitalized.  Since your code sample had it as blkref.layer it meant that either there was no Layer property available in a block reference or that you had used "layer" somewhere.
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 23, 2006, 06:32:01 PM
Also, just a question, but do you prefer the inline err handling or the err handler ocated elsewhere?  I normally use the handler located somewhere else, and resume back if I can fix the problem.  Select case is good for this.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 07:04:03 PM
Ok. You guys are a HUGE help to me. I am making some sort of progress here. The "InsertButton" appears to be working as far as the layer goes and as far as the space (CVPORT) goes.

Any further suggestions on the InsertButton part of the routine or any of the routine is appreciated.

I shall now look into some suggestions for error handling.

Code: [Select]
Sub InsertButton_Click()
Dim inspnt As Variant
Dim blkref As AcadBlockReference
Dim curLayer As String
curLayer = ThisDrawing.ActiveLayer.Name
Dim objLayer As AcadLayer
Dim curSpace As AcadBlock

If ThisDrawing.GetVariable("CVPORT") = 1 Then
   Set curSpace = ThisDrawing.PaperSpace
Else
   Set curSpace = ThisDrawing.ModelSpace
End If
'''
On Error GoTo err_han
'''
    For Each objLayer In ThisDrawing.Layers
        If 0 = StrComp(objLayer.Name, blkLayer, vbTextCompare) Then
        ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
        Else: ThisDrawing.Layers.Add blkLayer
        ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
        End If
    Next objLayer
    For Each objLayer In ThisDrawing.Layers
        If objLayer.Name = blkLayer Then
        objLayer.color = blkColor
        objLayer.Linetype = blkLType
        End If
    Next objLayer
    '''you should have something here to verify the block can be loaded.
    Me.hide
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    If ThisDrawing.ActiveSpace = acModelSpace Then
      Set blkref = curSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkref.layer = blkLayer
    Else
      Set blkref = curSpace.InsertBlock(inspnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkref.layer = blkLayer
    End If
    ThisDrawing.ActiveLayer = ThisDrawing.Layers(curLayer)
    ThisDrawing.Application.Update
Unload Me

Exit Sub

err_han:
Debug.Print Err.Number & Err.Description
   Exit Sub
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 07:25:54 PM
Also, just a question, but do you prefer the inline err handling or the err handler ocated elsewhere?  I normally use the handler located somewhere else, and resume back if I can fix the problem.  Select case is good for this.
I prefer to keep this type of error, where I KNOW it will probably throw an error at some point, in-line as I showed. For catastrophic or other errors I use an error handler to direct traffic. 

I've seen it done all in-line, which I do not like; all sent to the error handler, which is fine; and as a combination of both which I prefer. But I am just a self taught overachiever so I'm sure someone can detail why you should use ONLY one of the other 2 ways. :-)

Tim, not the last code you posted but the one before that Bob was commenting on. You have ....Layer.Item(blkLayer) for BOTH sides of the IF statement. The second one of them must be ADD, which is what CmdrDuh was trying to say.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 07:41:55 PM
Here is my rendition of the Insert button code:
Code: [Select]
Sub InsertButton_Click()
Dim inspnt As Variant
Dim blkref As AcadBlockReference
Dim layer As AcadLayer
Dim curSpace As AcadBlock
Dim oBlk As AcadBlock

On Error Resume Next
    Set layer = ThisDrawing.Layers.Item(blkLayer)
    If Err.Number <> 0 Then
        Err.Clear
        Set layer = ThisDrawing.Layers.Add(blkLayer)
        layer.color = blkColor
        layer.Linetype = blkLType
    End If
    '''you should have something here to verify the block can be loaded.
    ''OK, here's that check
    Set oBlk = ThisDrawing.Blocks.Item(blkName)
    If Err Then
        ''You'd better know that this Path will work, otherwise other precautions should be added
        blkName = MyPath & blkName & ".dwg"
        Err.Clear
    End If
   
On Error GoTo err_han
   
    If ThisDrawing.GetVariable("CVPORT") = 1 Then
       Set curSpace = ThisDrawing.PaperSpace
    Else
       Set curSpace = ThisDrawing.ModelSpace
    End If
   
    Me.hide
    inspnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    Set blkref = curSpace.InsertBlock(inspnt, blkName, 1, 1, 1, 0)
   
    blkref.layer = blkLayer

    ThisDrawing.Application.Update
    Unload Me

Exit Sub

err_han:
    MsgBox "There was an unhandled error. See the Immediate window in VBAIDE"
    Debug.Print vbCr & Err.Number & Err.Description
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 07:48:05 PM
Your Code with comments
Code: [Select]
Sub InsertButton_Click()
'Personal preference but I always like to have at least one capital in my variables
'It helps me find typos
Dim insPnt As Variant
Dim blkRef As AcadBlockReference
Dim curLayer As String
curLayer = ThisDrawing.ActiveLayer.Name
'Don't need above two lines because you aren't changing the layer anymore
Dim objLayer As AcadLayer
Dim curSpace As AcadBlock

If ThisDrawing.GetVariable("CVPORT") = 1 Then
   Set curSpace = ThisDrawing.PaperSpace
Else
   Set curSpace = ThisDrawing.ModelSpace
End If
'''
On Error GoTo err_han
'''
    For Each objLayer In ThisDrawing.Layers
        If 0 = StrComp(objLayer.Name, blkLayer, vbTextCompare) Then
        ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
        Else: ThisDrawing.Layers.Add blkLayer
        ThisDrawing.ActiveLayer = ThisDrawing.Layers(blkLayer)
        End If
    Next objLayer
'The above For Next section checks each layer to see if it matches blklayer
'If not it adds blklayer and sets it current but it does it as many times as there are layers
'You don't need this section anyway because you don't need to set the layer, Lets just add
'the layer in.  If it exists it doesn't hurt, if not we'll make it
    
    For Each objLayer In ThisDrawing.Layers
        If objLayer.Name = blkLayer Then
        objLayer.color = blkColor
        objLayer.Linetype = blkLType
'you should do an Exit For here to end the loop as a general rule
        End If
    Next objLayer
    '''you should have something here to verify the block can be loaded.
    Me.hide
    insPnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    If ThisDrawing.ActiveSpace = acModelSpace Then
      Set blkRef = curSpace.InsertBlock(insPnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkRef.layer = blkLayer
    Else
      Set blkRef = curSpace.InsertBlock(insPnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
      blkRef.layer = blkLayer
    End If
'This doesn't need to be If Then anymore using curSpace
    ThisDrawing.ActiveLayer = ThisDrawing.Layers(curLayer)
'You don't have to reset the layer as you didn't change it
    ThisDrawing.Application.Update
Unload Me

Exit Sub

err_han:
Debug.Print Err.Number & Err.Description
   Exit Sub
'Your error handling is very basic but is also servicable I would wait for a bit
'before you get deeper into error control
End Sub

And with my comments applied it comes out like
Code: [Select]
Sub InsertButton_Click()
Dim insPnt As Variant
Dim blkRef As AcadBlockReference
Dim objLayer As AcadLayer
Dim curSpace As AcadBlock
Dim booLayer As Boolean

On Error GoTo err_han

  If ThisDrawing.GetVariable("CVPORT") = 1 Then
    Set curSpace = ThisDrawing.PaperSpace
  Else
    Set curSpace = ThisDrawing.ModelSpace
  End If
    
  Set objLayer = ThisDrawing.Layers.Add(blkLayer)
    objLayer.color = blkColor
    objLayer.Linetype = blkLType
  
  Me.hide
  insPnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick Insertion Point: ")
    Set blkRef = curSpace.InsertBlock(insPnt, MyPath & blkName & ".dwg", 1, 1, 1, 0)
    blkRef.layer = blkLayer
    ThisDrawing.Application.Update
Unload Me

Exit Sub

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

Look it over and see if you understand what I have done and more importantly, why I did it.  If not, please ask away.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 07:52:22 PM
and just as a side note, the ACADLAYER object doesn't have a Color property, only a TrueColor property which is a lot less friendly to use.  Of course, if you pretend like you don't know that and use the non-existant Color property, it works swell but keep in mind when you do that that no matter what seems to be happening in your drawing, you can't do what you're doing.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 07:52:31 PM
Jeff,

Excellent. So, in my attempt to understand....and at the time you posted the response about the "add", I was on AfraLisp and reading about error trapping in VBA.

So, am I right when it appears the "On Error Resume Next" is similar to an if statement in that "if" blkLayer is not present...an error...which creates an error code that is anything other than 0, then clear the error code for future error code handling and create the blkLayer layer.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 08:05:56 PM
That is correct.  I still think you just ignore whether it's there or not and make it.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 08:06:38 PM
Wahr,

Awesome. I see just how gnarly the code I have been piecing together looks especially when compared to your rendition.

As far as layer creation. I use something similar in most of our lisp routines. Basically the lisp routine creates/recreates the layer for each use of the routine as the layer should be what the standard says it should be, so... there should be no worry about redefining the layer properties.

I understand no longer needing the curLayer anymore as the blkref.layer puts the block on the blkLayer. Also applies to the "For Each objLayer In ThisDrawing.Layers" section you described.

Reading on.....
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 23, 2006, 08:09:23 PM
Gnarly is something that happens to code especially when you are experimenting with something new.  I have more than once fought my way through to the end of a program, then scrapped it and redid it from scratch just to get rid of legacy crap, sloppy coding, and bad ideas.

I'm gone for the day but will check back in the AM.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 08:12:53 PM
Thanks Bob and Jeff. You guys have been Extremely helpful.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 08:29:58 PM
...but keep in mind when you do that that no matter what seems to be happening in your drawing, you can't do what you're doing.
Could you clarify this, Bob? It appears to me that it does do what he's doing....for now. Granted, ACAD no longer has a color property but it does still accept it, I'm sure for legacy purposes. But how long before they remove that backward compatibility????

Maybe what you were saying is that, although it seems to be working as desired now, don't be a bit surprised when it stops and pukes on your new tennis shoes? Which brings us back to that Error_Trap......

Quote from: akdrafter
so... there should be no worry about redefining the layer properties.
I guess that's why I always do it the way I do. I work with a number of different firms and I must make sure to use whatever each one has already done. If I'm adding the layer, then I make it the same for each one, but I never alter their existing layers.
One thing to be careful of, though. While you CAN use the ADD method without worry of one already being there in the Layers collection, doing the same thing with 'most' other collections will generate an error. It's another one of those "you gotta know when to use it" things that comes with experience.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 23, 2006, 08:50:06 PM
Jeff,

Concerning the layer thing. What your saying is that using the "add" method will leave the properties of the layer as is if it exists but set the layer properties if the layer does not exist?

I guess for my case it does not really matter as I am controlling the properties of the layer the block is to be inserted on via the text file. Which brings up a thought I have for this app once it gets further along. A check box for "existing" items, which once checked will use a different set of text files that set the layer properties for "existing". Basically a different layer name and color.

So, anyway....another day ends and I am headed home. Thanks for all the help and I will think about what the next step should be.

Thanks again.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 23, 2006, 11:18:39 PM
Jeff,

Concerning the layer thing. What your saying is that using the "add" method will leave the properties of the layer as is if it exists but set the layer properties if the layer does not exist? Sort of...If the layer exists, Add does nothing other than return the Layer object. If it does not exist, it is created with the default color/linetype/etc.

I guess for my case it does not really matter as I am controlling the properties of the layer the block is to be inserted on via the text file. Which brings up a thought I have for this app once it gets further along. A check box for "existing" items, which once checked will use a different set of text files that set the layer properties for "existing". Basically a different layer name and color.

So, anyway....another day ends and I am headed home. Thanks for all the help and I will think about what the next step should be.

Thanks again.
You're welcome for the help I can give. For the Existing, consider just adding that info to the original descriptions.......Block1-1,BLOCK1,LAYER1,1,CONTINUOUS,EXLAYER,EXCOLOR ....and adjusting the code to suit that. You may note that I try to minimze calls to outside files as much as possible. :-)
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bryco on May 23, 2006, 11:54:23 PM
 Unload Me, isn't included in the error handling, is this a problem?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 24, 2006, 01:01:28 AM
  is this a problem?
Yes.....but in the infamous words of Bob W. "I was waiting for..." akdrafter to notice.  :whistle:
Title: Re: Total newby to the swamp... VB block insertion
Post by: Kerry on May 24, 2006, 02:01:47 AM
heheheheheheeehe
 :lmao:
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 24, 2006, 10:23:09 AM
...but keep in mind when you do that that no matter what seems to be happening in your drawing, you can't do what you're doing.
Could you clarify this, Bob? It appears to me that it does do what he's doing....for now. Granted, ACAD no longer has a color property but it does still accept it, I'm sure for legacy purposes. But how long before they remove that backward compatibility????
Maybe what you were saying is that, although it seems to be working as desired now, don't be a bit surprised when it stops and pukes on your new tennis shoes? Which brings us back to that Error_Trap......

Quote from: akdrafter
so... there should be no worry about redefining the layer properties.
I guess that's why I always do it the way I do. I work with a number of different firms and I must make sure to use whatever each one has already done. If I'm adding the layer, then I make it the same for each one, but I never alter their existing layers.
One thing to be careful of, though. While you CAN use the ADD method without worry of one already being there in the Layers collection, doing the same thing with 'most' other collections will generate an error. It's another one of those "you gotta know when to use it" things that comes with experience.

I was just, in my usual roundabout semi-serious way, pointing out that the color property no longer officially exists and although it still works for layers in 2006, it may not in 2007 (haven't tested) or future releases.  I believe that, like the Add method, I have run across a few things that will toss an error if you try to use Color instead of TrueColor.  I for one would prefer that Adesk add the Color property back in officially for everything.  If there are 256 colors available in ACAD, let me use them.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 24, 2006, 12:39:12 PM
for the err_han.....like this?

Code: [Select]
err_han:
Debug.Print Err.Number & Err.Description
Unload Me
  Exit Sub
End Sub

As far as the color/true color property you guys speak of, is there a "prefered" way of defining this...

"Dim blkColor As Integer"

"objLayer.color = blkColor"

Bob,

Why is the "booLayer" dimensioned as true/false (boolean)?

Back to my lines and circles...
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 24, 2006, 12:50:12 PM
Slight change of subject....but got to thinking of color when the "True Color" came up....here is some true color for ya.

Yeah baby. Nuttin like gettin bent by one of these. :-)
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 24, 2006, 01:00:24 PM
I guess as it keeps coming up and rightly so when it does, we might as well start hitting on the error trapping.  When an error happens, there are a few things that you can do with it. 

The first that I'll mention is to ignore it.  This is normally a really, REALLY bad thing to do.  As you can see with Jeff's use on On Error Resume Next followed by some in line handling, there are times that it is fine to do.  Of course, in his example, you aren't actually ignoring the error, you are just telling the program to ignore it for long enough for you to fix it.

Secondly, you can cut and run.  That is what you are doing with your error handling now.  If there is an error of any sort, stop the program where it is and get out immediately.  This works sufficiently for most problems. But...

Thirdly, as you can see with Jeff's layer checking example, there are times that you want to use an error.  There are also times that errors can occur under certain circumstances that won't really affect your program in a bad way and can be ignored or errors that can be fixed when they occur and the program can continue.

What I generally use for my error handling is something that I picked up from Randall
Code: [Select]
Exit_Here:
  Exit Sub
err_han:
  Select Case err.Number
    Case -14663844 'arbitrary number I typed in for an example this error can be ignored
      err.clear
      Resume Next ' Resumes the program on the next line of code after the one that generated the error
    Case -18284765 'This is an error that we know how to fix
      'We do what needs to be done to fix it here
      err.Clear
      Resume ' Note: this will resume the program on the same line that broke it before
    Case Else 'If any other error occurs, get the duck outta Fodge
      debug.print err.number & " " & err.description
      Msgbox "It Broke",vbcritical, "Boom"
      err.clear
      goto exit_here
  End Select


AK, Good catch (the boolayer (hell, the trout too for that matter)) I started to do something different than I ended up doing with your code, then changed course but didn't entirely clean up after myself.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 24, 2006, 01:01:00 PM
What kind of fish do you need the sidearm for?
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 24, 2006, 01:16:15 PM
My wife said that she wants me home after every fishing trip...which I do a lot...uhhh...fish and come home for that matter. hahahahah... I draw just about every day, but so far have ever only drawn that piece of hardware once and only because what I was aiming at was 15' from me and wasn't expecting my company. Althought we were both fishing at the time. I think I caught more that day than it did. And not for nuttin... but some of our fish up here are actually shot before you can get them into the boat. Halibut... Baby got back... Anyway......

Ok, So I am sitting here trying to think of what kind of errors one might run into when using this particular type of app. I think I need to write them down and see how it affects what the app is doing, if I can resolve the error and continue or if the error makes the rest of the code unusable.

The "-#######" error numbers....are those error codes and if so are they documented somewhere?

Thanks for the input Bob.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 24, 2006, 01:23:10 PM
The numbers I used were randomly typed.  I have seen some really sparse documentation on error numbers but never anything super comprehensive.  What I generally do is start with
Code: [Select]
Exit_Here:
  Exit Sub
err_han:
  Select Case err.Number
    Case Else 'If any other error occurs, get the duck outta Fodge
      debug.print err.number & " " & err.description
      Msgbox "It Broke",vbcritical, "Boom"
      err.clear
      goto exit_here
  End Select
When errors occur, I figure out what is causing the error and what I can/should do with it.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 24, 2006, 01:32:35 PM
So, basically I need to test and test to make sure it works and make it fail if I can, then I will know how to deal with those errors.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 24, 2006, 01:36:16 PM
No matter how extensively you test and how many different things you try, when you release it for use, someone will break it immediately.  The best you can hope for in that case is that the error can be reproduced so that it can be fixed.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 24, 2006, 02:45:05 PM
My $0.02......I agree with Bob on how to find the errors you need to trap. Yes, you basically need to try to break it to be able to fix it.

One thing I know will fail within 5 minutes of a user testing it, they will initiate a transparent command, such as Pan, when you are requesting the insertion point....an unhandled error is raised so you, not so nicely, just dump them out......they go "What da .....????!!!! TTTTIIIIIIMMMMMMM, what'd ya do? I can't zoom or pan or nuttin' I think you broke my Autocad!!! WHAHHHHH!!!" Well, you get the idea..... :-)

Look around for a modified GetPoint....I think Bryco has posted one here that was a derivitive of an RR function that I have somewhere.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 25, 2006, 12:49:15 PM
akdrafter,

Moving on without you somewhat, I've done a little bit of rearranging on your Userform_Initialize Sub.  First I will post it as you have it with my comments.  Then I will post how I would do it.


Code: [Select]
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)
'Redimming three arrays to 999 just to make space uses memory unneccesarily
'It will also cause you program to crash if there are 1001 or more blocks
'Why not just redim the array as needed when needed
fFile = FreeFile


'Do you really need all three lists filled out at the beginning?
'If the user is picking a block from List 1, the time and memory used to
'populate all 3 arrays is 2/3 wasted, I would move these down to the click subs
'**** 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 this Sub is running, OptionButton1 was clicked so this will always
'be true, there's no reason for the IfThen check
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

Code: [Select]
Private Sub UserForm_Initialize()
  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()
Dim arrType1() As String
Dim arrTmp As Variant
Dim strTmp As String
Dim fFile As Integer
Dim I As Integer

fFile = FreeFile

  Open MyPath & "_Type1.txt" For Input As fFile
  For I = 0 To 4
    Line Input #fFile, strTmp
  Next
  I = 0
  While strTmp <> ""
    ReDim Preserve arrType1(0 To I)
    arrType1(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
  Wend
  Close fFile
  ReDim arrName1(0 To UBound(arrType1), 0 To 4)
  'Redim this to ubound(arrtype1) and they will always match without a doubt
  'I - 1 should be the same always but there's no chance that ubound(arrtype1) won't be ubound(arrtype1)
  'make sense?
  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
  ListBox1.Clear
  ListBox1.List = arrName1
  ListBox1.ListIndex = 0
End Sub

Private Sub OptionButton2_Click()
Dim arrType2() As String
Dim arrTmp As Variant
Dim strTmp As String
Dim fFile As Integer
Dim I As Integer

fFile = FreeFile

  Open MyPath & "_Type2.txt" For Input As fFile
  For I = 0 To 4
    Line Input #fFile, strTmp
  Next
  I = 0
  While strTmp <> ""
    ReDim Preserve arrType2(0 To I)
    arrType2(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
  Wend
  Close fFile
  ReDim arrName2(0 To UBound(arrType2), 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
  ListBox1.Clear
  ListBox1.List = arrName2
  ListBox1.ListIndex = 0
End Sub

Private Sub OptionButton3_Click()
Dim arrType3() As String
Dim arrTmp As Variant
Dim strTmp As String
Dim fFile As Integer
Dim I As Integer

fFile = FreeFile

  Open MyPath & "_Type3.txt" For Input As fFile
  For I = 0 To 4
    Line Input #fFile, strTmp
  Next
  I = 0
  While strTmp <> ""
    ReDim Preserve arrType3(0 To I)
    arrType3(I) = strTmp
    I = 1 + I
    Line Input #fFile, strTmp
  Wend
  Close fFile
  ReDim arrName3(0 To UBound(arrType3), 0 To 4)
  For I = 0 To UBound(arrType3)
    arrTmp = Split(arrType3(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
  ListBox1.Clear
  ListBox1.List = arrName3
  ListBox1.ListIndex = 0
End Sub
As you can see, there's really very little that I changed in your code, mostly what I did was rearrange it.  Do you understand why and how I did what I did?
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 25, 2006, 02:05:38 PM
Bob,

To answer your question... To some extent I do understand what you are saying.

As far as memory goes. The only memory used is when the user selects a type. So, if the user only selects type1, then only type1 will be stored in memory. However, if the user selects type1, type2 and type3 "OptionButton"s before they finally make up their mind, then all 3 would be stored in memory which would be the equivalent of having those types preloaded into memory at the begining of the app.

When we start the app we simply tell it that "OptionButton1" has been clicked(True) so that the list box is populated.

The line "I = 0 To 4" ("I" being an integer enabled of going into the billions of places and being a whole number only "Long") is simply telling the app to not read the first four lines of the text file and to start at line #5.

Then after setting the "read" of the text file to start at the correct line....Line 5, we now start a "while" there is no blank space(the end of the text file) we are going to start taking each line of the text file and converting it into a string...."strTmp". The line "I = 1 + I" keeps moving down each line of the text file until a blank space is reached... "While strTmp <> """

So, now that we have our "strTmp" filled with the contents of the text file we are ready to move on to the separating of the pertinent items within that text file.

Now here is where I get fuzzy. I am not sure what the difference is in "arrType#" and "arrName#". I think......in Jeff's version of the code the app would Dim the "arrType#" to set it to a string. Then, ReDim the "arrType#" to store 1,000 places in memory. But, what you are doing is telling the app that the values in "arrName#" are always going to be the same as those in "arrType#" so there is no need to ReDim "arrType#".

The rest of the code I understand is separating the contents of the array into columns for later use by the insertion function.

So in a nutshell I believe what you have done is to... "Do only what is needed when it is needed and simplify things"... Oh... and there is more than one way to skin the cat. :-)
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 25, 2006, 02:24:16 PM
Bob, so everytime an option button is clicked the file is re-read? I can see placing each file read into when the option button is selected, but once the array is filled there's no reason to read it again.....This isn't that big a deal for a "Insert one block" routine, but it was mentioned that multiple insertions/selections may be an option.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 25, 2006, 02:36:39 PM
Oh, and I don't want to argue about this, but I want to point out what RR said about Dynamic arrays. This is paraphrased from the class I took from him a few years back:
Quote from: more_or_less
Unless you KNOW you are going to have a small i.e.-less than 20 members, array you should never use ReDim in a loop. It is preferrable to either Redim it first to a value larger than it could possibly ever get to OR Set it to a reasonable number and check to make sure it isn't going to be exceeded with the next useage and increase it another chunk. In both cases a Redim to the final size is required.
I recall this quote solely due to I turned in a solution with a Redim Preserve in a loop and I was quite well chastized for it......

In Tim's case here, he used an initial value of 999 knowing he'd never have that many blocks in the array.

With the speed of today's computers it may not make any difference which method is used, and I can say that I have used all 3 methods without any noticable performance hit or gain.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 25, 2006, 02:54:11 PM
Definitely lots of ways to skin cats and most of them are ridiculous amount of fun.  What the first version is doing with

Dim arrType1() As String

is telling the program that you will be using a variable called arrType1, that it is going to be an array(), and that the values of the array will be strings.  You are not telling the program how many items the array is comprised of just that it is an array.  When you Dim a variable as a string, the program automatically reserves 10 bytes of memory for that variable.  When you Dim something as an Array, you reserve 20 bytes plus 4 bytes per Dimension plus the data size.  This means that right off the bat with the above Dim, you are reserving 34 bytes of memory.  When you do

ReDim arrType1(0 to 999)

you now have 20 bytes for the array + 4 because it is a single dimension Array + (10 * 999) bytes for the reserved strings.  That's a total of 10,014 bytes.  I know that 10k RAM isn't much these days, but you are doing it three times so you have 30k used and the arrays are still empty, their size only goes up from here.  Of course, after you populate them, you are chopping off the unused portion and therefore freeing the memory back up but it is still being used for no reason for a while.  What I proposed is that you ReDim the array to be one item longer everytime you are going to add an item so that the array is always the length that you need.  By way of example, the strings in the txt file are 35 characters long.  Even though block names will vary I'm sure, lets go with 35 for the example.  If there were 100 blocks in each list, with the way you were doing it you would have:

10,014 bytes reserved for the array
35 characters X 2 bytes per character X 100 blocks in the list or 7000 bytes
17,014 bytes X 3 arrays
51,042 bytes total used to populate the three lists

Still not an enormous amount but compared with 8,024 bytes to fill one list as needed and you can see that it starts to add up.

Next with the way you had it, after you were finished populating the array, you were cropping it with

Redim Preserve arrType1(0 to I - 1)

followed by redimming one dimension the arrName array the same way which ends up making the first dimension of arrName the same length as arrtype.  With my suggestion, instead of using I - 1 to set the ubound, since arrType is set as we went and is the right length, so we use the ubound of arrType to set the ubound of the first dimension of arrName.

Let me know if this actually makes sense to you because I was really feeling like I was starting to talk in circles.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 25, 2006, 02:57:24 PM
Bob, so everytime an option button is clicked the file is re-read? I can see placing each file read into when the option button is selected, but once the array is filled there's no reason to read it again.....This isn't that big a deal for a "Insert one block" routine, but it was mentioned that multiple insertions/selections may be an option.
Good point.  I initially changed it to have the populate list routines as separate Subs then changed again to be in the click sub.  Would be easy to check and see if the array was empty and only populating it if so.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 25, 2006, 03:00:01 PM
Oh, and I don't want to argue about this, but I want to point out what RR said about Dynamic arrays. This is paraphrased from the class I took from him a few years back:
Quote from: more_or_less
Unless you KNOW you are going to have a small i.e.-less than 20 members, array you should never use ReDim in a loop. It is preferrable to either Redim it first to a value larger than it could possibly ever get to OR Set it to a reasonable number and check to make sure it isn't going to be exceeded with the next useage and increase it another chunk. In both cases a Redim to the final size is required.
I recall this quote solely due to I turned in a solution with a Redim Preserve in a loop and I was quite well chastized for it......

In Tim's case here, he used an initial value of 999 knowing he'd never have that many blocks in the array.

With the speed of today's computers it may not make any difference which method is used, and I can say that I have used all 3 methods without any noticable performance hit or gain.
I don't remember that but it wasn't my chastizement and he does have a way of making you remember when you do something in a way other than the best way.
Quote
In Tim's case here, he used an initial value of 999 knowing he'd never have that many blocks in the array.
never say never
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 25, 2006, 08:27:41 PM
Okie dokie. Check it out. I have attached the code as I think it is a bit long to be posting anymore.

There has been a function added to "search" the support paths...way cool....and thanks to "E"(not sure he wants to be named...hahahaah), he knows who he is.

Anyway. The routine appears to be working. So now, I think I am going to dress up the GUI a bit while I think of what other "options" would be useful.

Thank you Bob & Jeff.
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 26, 2006, 10:01:41 AM
Elmo?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 26, 2006, 11:06:18 AM
I remember that search function.  It was the first of if I remember correctly 2 functions that were my extremely badly failed attempt at getting people to provide similar functionality between lisp and VBA over at CAD Vault.  I did this one, and someone, I think maybe Kerry although it could have been Stig but was probably someone else entirel, posted a lisp function.  Then it went down in a hail of bullets and bad feelings.  Good times that.  Either way, this Elmo guy jacked my function.  Bastard.

As for dressing up the IDE, how can you do more than you already have?  Seriously though, you mentioned adding fuctionality to change the layer to an exiting layer so a checkbox for that might in order.  It would also be nice to have a preview pane.  No matter how descriptive a block's name is, somebody is going to be unable to figure out what it is without a picture.  A layer override might be a good idea.  I know that occasionally I will want/need to use a typically plan symbol in a detail and insert it, then change the layer.  You could do this with either a textbox to type the layer, or a combobox that lists detail layers.

Just throwing out ideas here, not trying to drive.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 26, 2006, 12:46:57 PM
Bob,

Elmo? Nice.  :-D I am sure he likes that one. As far as your code going down in a hail of bullets, the code must have been wearing a BPV because it survived and I think it works very nicely. Why do I say that, well... it works. I can be pretty simplistic like that, but a fair amount of time simple is better.

So, movin on....

There are things I would like to add like:

displaying the current dimscale(Got this one, just have not added it to the GUI yet)

a check on the "insunits"(what a pain this one can be) freaks me out when I think a block should be inserted a certain size and the "insunits" are set to feet and the block I am inserting comes in haaaaauuuugggeeeee. I think "insunits" set to "0" would be best..... a check of, store value, switch to 0, and after completion a restoration of the original "insunits" value.

Yes.... a check box for "Existing" (layer)

Yes... Preview pain, not sure how to tackle that one. I mean, I have heard of a couple of different ways of doing it but not sure which makes the most sense. Relying on an separate program is not one that I find appealing. Slides? would it work? Bitmap's/Png's...that could create a lot of extra files. Will have to explore that one in depth a little further.

Layer overide... I had not thought of that one and I do insert some of these "Plan" blocks into "Detail" sheets. Good thought. Maybe another check box for "Details" with the default on start up being "Plan". I think on that one.... simply checking a "Details" check box that would then read a text file like it is already doing, with the layers defined that way. Open to suggestions though.

So.....yeah.... lots of ideas and all are great and appreciated. Keep em comin.

Thanks.

Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 26, 2006, 01:18:57 PM
Yes... Preview pain, not sure how to tackle that one. I mean, I have heard of a couple of different ways of doing it but not sure which makes the most sense. Relying on an separate program is not one that I find appealing. Slides? would it work? Bitmap's/Png's...that could create a lot of extra files. Will have to explore that one in depth a little further.
bitmaps stored in a DLL file would work.  Then you would only need to keep up with 1 file
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 26, 2006, 01:37:58 PM
Cmdr,

I tried the bitmaps in a dll once....learned how to do it from Afralisp... and it worked fine here in this office but for whatever reason our other office had continual problems with it. I am not sure why but I think it might have had something to do with permissions on their server...me not having sufficient permissions. Anyway, I think you are right though a bitmap dll would be a good way to go.

Thanks for the input.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 26, 2006, 01:55:55 PM
Bob,

Elmo? Nice. :-D I am sure he likes that one. As far as your code going down in a hail of bullets, the code must have been wearing a BPV because it survived and I think it works very nicely. Why do I say that, well... it works. I can be pretty simplistic like that, but a fair amount of time simple is better.

So, movin on....

There are things I would like to add like:

displaying the current dimscale(Got this one, just have not added it to the GUI yet)

a check on the "insunits"(what a pain this one can be) freaks me out when I think a block should be inserted a certain size and the "insunits" are set to feet and the block I am inserting comes in haaaaauuuugggeeeee. I think "insunits" set to "0" would be best..... a check of, store value, switch to 0, and after completion a restoration of the original "insunits" value.
Good idea.  This should get 'er
Code: [Select]
Public intInsUnits As Integer

Private Sub UserForm_Initialize()
  intInsUnits = ThisDrawing.GetVariable("INSUNITS")
  ThisDrawing.SetVariable "INSUNITS", 0
  OptionButton1.Value = True
End Sub
Private Sub UserForm_Terminate()
  ThisDrawing.SetVariable "INSUNITS", intInsUnits
End Sub
Quote
Yes.... a check box for "Existing" (layer)

Yes... Preview pain, not sure how to tackle that one. I mean, I have heard of a couple of different ways of doing it but not sure which makes the most sense. Relying on an separate program is not one that I find appealing. Slides? would it work? Bitmap's/Png's...that could create a lot of extra files. Will have to explore that one in depth a little further.
Cat skinning and where to start

Quote
Layer overide... I had not thought of that one and I do insert some of these "Plan" blocks into "Detail" sheets. Good thought. Maybe another check box for "Details" with the default on start up being "Plan". I think on that one.... simply checking a "Details" check box that would then read a text file like it is already doing, with the layers defined that way. Open to suggestions though.
that could work.
Quote

So.....yeah.... lots of ideas and all are great and appreciated. Keep em comin.

Thanks.


Oh, I will.
Title: Re: Total newby to the swamp... VB block insertion
Post by: akdrafter on May 26, 2006, 02:17:29 PM
Ok, looking at that code snippet learned me somethin....

 intInsUnits = ThisDrawing.GetVariable("INSUNITS")
 ThisDrawing.SetVariable "INSUNITS", 0


The above is the VBA equivalent of a lisp type thang....(setq YADDA(getvar "INSUNITS"))

Private Sub UserForm_Terminate()
 ThisDrawing.SetVariable "INSUNITS", intInsUnits
End Sub

This is how I would "restore" all of those things that get tweaked at run time?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 26, 2006, 02:23:04 PM
The above is the VBA equivalent of a lisp type thang....(setq YADDA(getvar "INSUNITS"))
exactly
Quote

Private Sub UserForm_Terminate()
 ThisDrawing.SetVariable "INSUNITS", intInsUnits
End Sub

This is how I would "restore" all of those things that get tweaked at run time?
It's definitely one way.  Someone on CAD Vault made a variable save/restore class.  I think it was mmelone although I have a nagging feeling that it was Paul Marshall.  Maybe both, I can't remember.  I'll see if I can dig it up though.  If it's more than a handful of system variables that your changing, the class would probably be a better way.
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 26, 2006, 03:24:31 PM
If it's more than a handful of system variables that your changing, the class would probably be a better way.
Id be interested in seeing that.  I have one for LISP, but one for VBA would be sweet
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 26, 2006, 03:25:17 PM
would it be done with an array?  Thinking about it, that would be a pretty easy way to handle it.
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 26, 2006, 04:22:59 PM
My mistake, it was Mark who did it.  His username was simply Mark and I don't remember his last name.  If anyone else does, please post it so he gets the credit.
Code: [Select]
Option Explicit
' Setting saver is to ensure that users have their current settings saved
' and restored when custom VBA programs are done.

Private iOSMode As Integer ' Sets the object snap mode
Private iOrthoMode As Integer ' Turns ortho mode on or off
Private iSnapMode As Integer ' Turns snap on or off
Private iAttDia As Integer ' Controls whether a dialog is shown for attribute values
Private iAUnits As Integer ' Sets units for angles
Private iBlipMode As Integer ' Turns blips on or off
Private sCEColor As String 'Sets the color of new objects
Private dCELTScale As Double 'Sets the current object linetype scaling factor
Private sCELType As String 'Sets the linetype of new objects
Private dChamferA As Double 'Sets the first chamfer distance
Private dChamferB As Double 'Sets the second chamfer distance
Private dChamferC As Double ' Sets the chamfer length
Private dChamferD As Double ' Sets the chamfer angle
Private sCLayer As String ' Sets the current layer
Private iCMDEcho As Integer ' Controls whether AutoCAD echoes prompts and input during the AutoLISP (command) function
Private iEdgeMode As Integer ' Controls how TRIM and EXTEND determine cutting and boundary edges
Private iFileDia As Integer ' Suppresses display of the file dialog boxes
Private iGridMode As Integer ' Specifies whether the grid is turned on or off
Private iHighlight As Integer ' Controls object highlighting; does not affect objects selected with grips
Private dLTScale As Double ' Sets the global linetype scale factor
Private iLUnits As Integer ' Sets linear units
Private iLUPrec As Integer ' Sets the number of decimal places displayed for linear units
Private iMenuEcho As Integer ' Sets menu echo and prompt control bits
Private iMirrText As Integer ' Controls how MIRROR reflects text
Private iPDMode As Integer ' Controls how point objects are displayed
Private iPDSize As Double ' Sets the display size for point objects
Private iProjMode As Integer ' Sets the current Projection mode for trimming or extending
Private dSnapAng As Double ' Sets snap and grid rotation angle for the current viewport
Private vSnapBase As Variant ' Sets the snap and grid origin point for the current viewport relative to the current UCS
Private iSnapISOPair As Integer ' Controls the isometric plane for the current viewport
Private iSnapStyl As Integer ' Sets snap style for the current viewport
Private vSnapUnit As Variant ' Sets the snap spacing for the current viewport
Private iUCSIcon As Integer ' Displays the UCS icon for the current viewport
Private iUnitMode As Integer ' Controls the display format for units

Private Sub SaveCADSettings()

iOSMode = ThisDrawing.GetVariable("OSMODE")
iOrthoMode = ThisDrawing.GetVariable("ORTHOMODE")
iSnapMode = ThisDrawing.GetVariable("SNAPMODE")
iAttDia = ThisDrawing.GetVariable("ATTDIA")
iAUnits = ThisDrawing.GetVariable("AUNITS")
iBlipMode = ThisDrawing.GetVariable("BLIPMODE")
sCEColor = ThisDrawing.GetVariable("CECOLOR")
dCELTScale = ThisDrawing.GetVariable("CELTSCALE")
sCELType = ThisDrawing.GetVariable("CELTYPE")
dChamferA = ThisDrawing.GetVariable("CHAMFERA")
dChamferB = ThisDrawing.GetVariable("CHAMFERB")
dChamferC = ThisDrawing.GetVariable("CHAMFERC")
dChamferD = ThisDrawing.GetVariable("CHAMFERD")
sCLayer = ThisDrawing.GetVariable("CLAYER")
iCMDEcho = ThisDrawing.GetVariable("CMDECHO")
iEdgeMode = ThisDrawing.GetVariable("EDGEMODE")
iFileDia = ThisDrawing.GetVariable("FILEDIA")
iGridMode = ThisDrawing.GetVariable("GRIDMODE")
iHighlight = ThisDrawing.GetVariable("HIGHLIGHT")
dLTScale = ThisDrawing.GetVariable("LTSCALE")
iLUnits = ThisDrawing.GetVariable("LUNITS")
iLUPrec = ThisDrawing.GetVariable("LUPREC")
iMenuEcho = ThisDrawing.GetVariable("MENUECHO")
iMirrText = ThisDrawing.GetVariable("MIRRTEXT")
iPDMode = ThisDrawing.GetVariable("PDMODE")
iPDSize = ThisDrawing.GetVariable("PDSIZE")
iProjMode = ThisDrawing.GetVariable("PROJMODE")
dSnapAng = ThisDrawing.GetVariable("SNAPANG")
vSnapBase = ThisDrawing.GetVariable("SNAPBASE")
iSnapISOPair = ThisDrawing.GetVariable("SNAPISOPAIR")
iSnapStyl = ThisDrawing.GetVariable("SNAPSTYL")
vSnapUnit = ThisDrawing.GetVariable("SNAPUNIT")
iUCSIcon = ThisDrawing.GetVariable("UCSICON")
iUnitMode = ThisDrawing.GetVariable("UNITMODE")

End Sub

Private Sub RecallCADSettings()
ThisDrawing.SetVariable "OSMODE", iOSMode
ThisDrawing.SetVariable "ORTHOMODE", iOrthoMode
ThisDrawing.SetVariable "SNAPMODE", iSnapMode
ThisDrawing.SetVariable "ATTDIA", iAttDia
ThisDrawing.SetVariable "AUNITS", iAUnits
ThisDrawing.SetVariable "BLIPMODE", iBlipMode
ThisDrawing.SetVariable "CECOLOR", sCEColor
ThisDrawing.SetVariable "CELTSCALE", dCELTScale
ThisDrawing.SetVariable "CELTYPE", sCELType
ThisDrawing.SetVariable "CHAMFERA", dChamferA
ThisDrawing.SetVariable "CHAMFERB", dChamferB
ThisDrawing.SetVariable "CHAMFERC", dChamferC
ThisDrawing.SetVariable "CHAMFERD", dChamferD
ThisDrawing.SetVariable "CLAYER", sCLayer
ThisDrawing.SetVariable "CMDECHO", iCMDEcho
ThisDrawing.SetVariable "EDGEMODE", iEdgeMode
ThisDrawing.SetVariable "FILEDIA", iFileDia
ThisDrawing.SetVariable "GRIDMODE", iGridMode
ThisDrawing.SetVariable "HIGHLIGHT", iHighlight
ThisDrawing.SetVariable "LTSCALE", dLTScale
ThisDrawing.SetVariable "LUNITS", iLUnits
ThisDrawing.SetVariable "LUPREC", iLUPrec
ThisDrawing.SetVariable "MENUECHO", iMenuEcho
ThisDrawing.SetVariable "MIRRTEXT", iMirrText
ThisDrawing.SetVariable "PDMODE", iPDMode
ThisDrawing.SetVariable "PDSIZE", iPDSize
ThisDrawing.SetVariable "PROJMODE", iProjMode
ThisDrawing.SetVariable "SNAPANG", dSnapAng
ThisDrawing.SetVariable "SNAPBASE", vSnapBase
ThisDrawing.SetVariable "SNAPISOPAIR", iSnapISOPair
ThisDrawing.SetVariable "SNAPSTYL", iSnapStyl
ThisDrawing.SetVariable "SNAPUNIT", vSnapUnit
ThisDrawing.SetVariable "UCSICON", iUCSIcon
ThisDrawing.SetVariable "UNITMODE", iUnitMode

End Sub

Private Sub Class_Initialize()
Call SaveCADSettings
End Sub

Private Sub Class_Terminate()
Call RecallCADSettings
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 26, 2006, 05:17:34 PM
ok, I understand most  of that.  Why the Class_Init and Terminate at the bottom?
Why not just Call SaveCADSettings from your routine?
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 26, 2006, 05:19:06 PM
And was it a class module or just a module?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 26, 2006, 05:30:13 PM
I believe it's to automate it.  You initialize the class everything gets saved.  You terminate the class, everything gets unsaved.  This was saved out in my "bunch o' crap that looks like it will be hella usefull" folder.  I'll have to go through my ECC archive when I get to the house for more clarity on the matter.


****

Class module
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bryco on May 26, 2006, 05:42:44 PM
Mark Johnston.
Title: Re: Total newby to the swamp... VB block insertion
Post by: David Hall on May 26, 2006, 05:50:31 PM
not to ask stupid questions, but is Class_Initialize() a autocad builtin function or do you just run it like any other sub?
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on May 26, 2006, 05:58:01 PM
Thanks Bryco

Duh, when you initialize the class it would go.  Much like Userform_Initialize
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on May 26, 2006, 09:07:51 PM
cmdrDuh,
Place the code Bob posted in a Class module, name the Class via the Properties window to MyVars, then add a Standard Module with this code and step through it to see how it works.

Code: [Select]
Sub test()
Dim cMyvars As MyVars

Set cMyvars = New MyVars

''do whatever

Set cMyvars = Nothing
End Sub
Title: Re: Total newby to the swamp... VB block insertion
Post by: ScottBolton on June 14, 2006, 03:45:44 PM
I AM EXHAUSTED! Jeff pointed me here from discussion.autodesk and I've spent the whole day reading the thread. I think the only word I understood was "trout".  :cry:

Anyhoo I'll retrack and hopefully be considerably better at VBA at the end of it.

Regarding images, I found this trick on msdn.microsoft and it involves creating a library userform with lots of images on it. Then just change your preview image by referencing your library:

Preview.Picture = bTLibrary.Diag1003_4.Picture

Works a treat and no DLL in sight.

S
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on June 14, 2006, 03:49:15 PM
Scott, welcome to the fray.  Take small bites, it's easier to digest that way.  Ask all the questions that you want and/or need to.
Title: Re: Total newby to the swamp... VB block insertion
Post by: ScottBolton on June 14, 2006, 03:52:34 PM
Bob, this thread is already waaaaay too long for me to ask ALL the questions I want!

I am confident that I've found a new home though. Thanks for the welcome.

I'll be in touch - that you can count on!

S
Title: Re: Total newby to the swamp... VB block insertion
Post by: Bob Wahr on June 14, 2006, 04:01:08 PM
Bob, this thread is already waaaaay too long for me to ask ALL the questions I want!
Start new ones, lots of them.  How much experience with VBA do you have?  I ask because if you are a beginner, CmdrDuh has a VBA class going.  It's currently on hiatus and if I remember our last conversation correctly, about to be revamped but you might want to shoot him a PM (http://www.theswamp.org/index.php?action=pm;sa=send;u=332) and talk to him about it.
Title: Re: Total newby to the swamp... VB block insertion
Post by: ScottBolton on June 14, 2006, 04:19:40 PM
Bob, I have trouble spelling BVA - does that answer your question? Seriously I haven't a Scooby. Although all of my Lisp routines now adopt VBA for the GUI (isn't DCL clunky?) everything is passed to a tmp file then read back into Lisp where I'm as happy as Larry. I have yet to actually do something in VBA. All the Dims and the Privates leave me cold. I have two books that prop up my desk to just the right height. CD's class sounds good and I've sent a PM. Thanks for the tip.

S
Title: Re: Total newby to the swamp... VB block insertion
Post by: Jeff_M on June 14, 2006, 05:07:28 PM
Hi Scott,
I'm glad you found your way over here. As Bob said, don't be afraid to ask questions of any type. IIRC this thread was started due to it being akdrafter's first attempt at a VBA program. I know that he, too, came from a lisp background, so it CAN  be done.....heck, I came from lisp into VBA, also, and now I'm venturing out into the .NET world......

Anyway, welcome to theSwamp and make yourself at home.