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

0 Members and 1 Guest are viewing this topic.

Bob Wahr

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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Total newby to the swamp... VB block insertion
« Reply #61 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.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

akdrafter

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

Jeff_M

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

Jeff_M

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

Bob Wahr

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

Bob Wahr

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

akdrafter

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

Bob Wahr

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

akdrafter

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

Bob Wahr

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

akdrafter

  • Guest
Re: Total newby to the swamp... VB block insertion
« Reply #71 on: May 23, 2006, 08:12:53 PM »
Thanks Bob and Jeff. You guys have been Extremely helpful.

Jeff_M

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

akdrafter

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

Jeff_M

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