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

0 Members and 3 Guests are viewing this topic.

akdrafter

  • Guest
Total newby to the swamp... VB block insertion
« 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

deegeecees

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #1 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.

 :-)

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #2 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

deegeecees

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #3 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.

Barry Clark

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #4 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.
« Last Edit: December 15, 2005, 04:56:13 PM by Barry Clark »

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #5 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

Arizona

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #6 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?

hendie

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #7 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Total newby to the swamp... VB block insertion
« Reply #8 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 !
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Total newby to the swamp... VB block insertion
« Reply #9 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......

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #10 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.

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion..hacking away....
« Reply #11 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


akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #12 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.....

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #13 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?

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Total newby to the swamp... VB block insertion
« Reply #14 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.