Author Topic: New Project Step 2 Layers  (Read 21883 times)

0 Members and 2 Guests are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: New Project Step 2 Layers
« Reply #75 on: September 19, 2005, 03:40:38 PM »
'Next objLayer' primarily helps you, and anyone else looking at your code, see which FOR loop you are nexting.
Well Said!  I usually just use Next, but I was trying to code it so it made a little more sense.
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)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: New Project Step 2 Layers
« Reply #76 on: September 19, 2005, 03:52:33 PM »
I imagine Something like this ;
Code: [Select]
Option Explicit

Sub Test()
    Dim blnResult As Boolean
    blnResult = Testhelper("0")
   
    MsgBox blnResult
    '
    '
   
    blnResult = Testhelper("SpuriousLayerName")
   
    MsgBox blnResult
   
End Sub


Function Testhelper(LayerName As String) As Boolean
   Dim layerObj As AcadLayer
     
   On Error GoTo Test_Error
   
   Set layerObj = ThisDrawing.Layers.Item(LayerName)
 
   MsgBox layerObj.ObjectID
   
   Testhelper = True

   On Error GoTo 0
   Exit Function

Test_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Testhelper of Module Module1"
   
    Testhelper = False
End Function
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.

Bob Wahr

  • Guest
Re: New Project Step 2 Layers
« Reply #77 on: September 19, 2005, 03:53:28 PM »
The two ways I see to use the function in an If...Then Statement is to check if it's true or check if it's false.  Basically, if it exists, do something to it, or if it doesn't exist, make it, then do something to it, is what you would do for a true test.  If you just wanted to ensure that the layer exists in the drawing, testing for false would be easier.

For these, I am simply going with a layer name as type in on the command line.
Code: [Select]
Sub Test1()
'this sub will test for true and do things accordingly
Dim strLayName as String
strLayName = ThisDrawing.Utility.Getstring(1,"Type layer name to check: ")
'on the above line, the 1 in the first field in parenthesis tells the GetString method to allow spaces
if DoesLayerExist(strLayName) = True then
   'Do things to the layer if it exists
Else
  'The layer doesn't exist. Make the layer, then do things to it
End if
end sub
I went the long way with my If. DoesLayerExist(DLE) is a Boolean.  That means that that it has two states.  True/False is one way to think of it, the other way is that it either is or it isn't.  Think of a light in your house as a boolean.  If the light is on (True) there is light.  If the light is off(false) there is no light.  So there either is light or there isn't light.  The following code works exactly the same way but instead of checking to see if DLE is true, I am just checking to see if DLE is.
Code: [Select]
Sub Test2()
'this sub will test for true and do things accordingly
Dim strLayName as String
strLayName = ThisDrawing.Utility.Getstring(1,"Type layer name to check: ")
'on the above line, the 1 in the first field in parenthesis tells the GetString method to allow spaces
[b]if DoesLayerExist(strLayName) then[/b]
   'Do things to the layer if it exists
Else
  'The layer doesn't exist. Make the layer, then do things to it
End if
end sub
Now if all we want to do is make sure that the layer exists, and if it doesn't, make it, then we really don't care about the true case.  We could do
Code: [Select]
Sub Test3()
Dim strLayName as String
strLayName = ThisDrawing.Utility.Getstring(1,"Type layer name to check: ")
if DoesLayerExist(strLayName) then
   'Nothing here because we aren't doing anything to an existing layer
Else
  thisDrawing.Layers.Add(strLayName)
End if
end sub
But the whole wasted true field just takes up space, memory, and time.  Instead I'm going to check to see if DLE is false which I could do with "If DoesLayerExist(strLayName)=False Then" but prefer to do it by just checking to see if DLE is like this
Code: [Select]
Sub Test4()
Dim strLayName as String
strLayName = ThisDrawing.Utility.Getstring(1,"Type layer name to check: ")
If [b]Not[/b] DoesLayerExist(strLayName) Then
  ThisDrawing.Layers.Add(strLayName)
End if
End Sub
I tried to explain my methods and my thought process so Duh wouldn't have to but as I have a tough time following my thought processes most times, myself, if you don't understand what I am saying or doing, ask.

Bob Wahr

  • Guest
Re: New Project Step 2 Layers
« Reply #78 on: September 19, 2005, 03:54:22 PM »
Took me three tries to get past the "someone posted while your slow butt was typing" messages.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: New Project Step 2 Layers
« Reply #79 on: September 19, 2005, 04:07:35 PM »
Unless I'm mistaken,
If you just want to make sure the layer is there ;

 
Code: [Select]
Set layerObj = ThisDrawing.Layers.Add("New_Layer")will do the job, and wont complain if the Layer already exists.

The  layerObj will hold < either > the Existing or New Layer Object.

The TestForExisting would be required if you wanted to determine <say> its Color or lineType et al ..
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.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: New Project Step 2 Layers
« Reply #80 on: September 19, 2005, 05:43:42 PM »
Code: [Select]
Private Function DoesLayerExist(ByRef LayerName As String) As Boolean

 Dim objLayer As AcadLayer

 For Each objLayer In ThisDrawing.Layers
 If UCase(objLayer.Name) = UCase(LayerName) Then
 DoesLayerExist = True
 'Exit Function
 Else:
 DoesLayerExist = False
 End If
 Next objLayer
End Function
Mark, just curious why you commented out the Exit Function?  Did it run better without that piece?
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)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: New Project Step 2 Layers
« Reply #81 on: September 20, 2005, 08:14:37 AM »
*shrug* I can't remember David. I do know it shouldn't be though.
TheSwamp.org  (serving the CAD community since 2003)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: New Project Step 2 Layers
« Reply #82 on: September 20, 2005, 10:09:19 AM »
The reason I had that in there was so when the Function returned true, it would stop and return the value.  I think that without that, the function will always return false UNLESS the layer you are checking for happens to be the last one.  Guru's can correct me on that, but logically that seems correct.
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)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: New Project Step 2 Layers
« Reply #83 on: September 20, 2005, 10:10:40 AM »
Also, to explain a little more, the Next objLayer would continue even though the test came up True.  We need to end the Function so the result is returned.  Does that make more sense?
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)

Bob Wahr

  • Guest
Re: New Project Step 2 Layers
« Reply #84 on: September 20, 2005, 11:38:26 AM »
DoesLayerExist is false until set to true.  Since it is private, it won't retain it's value and will always start as false so we could leave the else out.  We should still IMO have the Exit Function though.  Think of it this way Mark.  Say there are 2000 layers in the drawing and it takes the program 5 seconds to check through them all (yes, a bit of exaggeration) If the function finds the layer it is looking for first, what's the point of iterating through the other 1999?  Let's just bug out and return the result.
Code: [Select]
Private Function DoesLayerExist(ByRef LayerName As String) As Boolean

Dim objLayer As AcadLayer

For Each objLayer In ThisDrawing.Layers
If UCase(objLayer.Name) = UCase(LayerName) Then
DoesLayerExist = True
Exit Function
End If
Next objLayer
End Function

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: New Project Step 2 Layers
« Reply #85 on: September 20, 2005, 11:45:48 AM »
Also, to explain a little more, the Next objLayer would continue even though the test came up True.  We need to end the Function so the result is returned.  Does that make more sense?

Yes it does.

Good explanation there Bob . . .
TheSwamp.org  (serving the CAD community since 2003)