Author Topic: If Layer Exists Then:  (Read 18511 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
If Layer Exists Then:
« Reply #15 on: February 18, 2005, 12:28:21 PM »
What about using Select Case to determine the scale factor, and then running the layer code from there
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)

ML

  • Guest
If Layer Exists Then:
« Reply #16 on: February 18, 2005, 12:52:00 PM »
Yes that is very possible, a matter of fact, probable.

I just don't know how to make it overlook the existing ones.

ML

  • Guest
If Layer Exists Then:
« Reply #17 on: February 18, 2005, 12:55:37 PM »
That would be a more efficient way to do it I'm sure but I think it would still give the same result.

I need a way to incorporate the layer code into my existing code to make this work, just not sure ( code wise) how to do it.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
If Layer Exists Then:
« Reply #18 on: February 18, 2005, 01:37:03 PM »
Well, if you used Select to sort what scale you chose, then use the layer code to see if it exists.  BTW, whats the harm in recreating the layer if it exists?  That way it creates on the fly as needed
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)

ML

  • Guest
If Layer Exists Then:
« Reply #19 on: February 18, 2005, 04:05:18 PM »
Well, something weird is going on and I can only assume it is because it is seeing that something already exists, thus the duplicate record message and the layers and up all weird.

After I get the error, it breaks on the layername (in the module) of the duplicate layer and highlights it yellow.

So, I can only assume that it is the duplicate layername that is causing the problem.

ML

  • Guest
If Layer Exists Then:
« Reply #20 on: February 18, 2005, 04:10:23 PM »
Suppose I have a block (Intercomm) inserted at 24, then I go and insert another one at 48, it is still going to pass through the code and see that there is a blocked scaled to 24 as well.

So, may be I need a way for it to know to only look at blocks that were just inserted.

That would probably involve an event procedure and I am not sure there is one for inserting blocks.

It seems like this could be a lot easier.

ML

  • Guest
If Layer Exists Then:
« Reply #21 on: February 18, 2005, 04:43:00 PM »
As inefficient as it would be, I wonder if I wouldn't be better off having 13 different modules in this one project, one for each scale?

When I do it that way, there is really no conflict but it just seems like a cop out to me, plus I need the module to load and run every time I insert a block, that might be very slow as well.

Anonymous

  • Guest
If Layer Exists Then:
« Reply #22 on: February 18, 2005, 06:26:43 PM »
Man, this module is kicking my   :shock:

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
If Layer Exists Then:
« Reply #23 on: February 18, 2005, 06:30:04 PM »
PMFJI, but are you trying to complete a number of tasks by mixing lisp and vba? If so, it seems like you are making it hard on yourself.....

None-the-less, to get the last entity which in this case appears to be a block insert, why not just get the last entity in modelspace?

Dim entLast As AcadEntity
Set entLast = ThisDrawing.ModelSpace.Item(ThisDrawing.ModelSpace.Count - 1)

Even after reading all of the questions and answers in this thread, I'm still not clear exactly what you are trying to accomplish.....but I still don't think that the '13 different modules' is the approach you need to take.

Instead of placing the blocks on a layer and then renaming the layer based on scale, why not just create the layer based on scale and then place the new block on that layer......

Or, to go with your OnEvent idea you could place something like this in the ObjectAdded event in the ThisDrawing module:
Code: [Select]

Private Sub AcadDocument_ObjectAdded(ByVal Object As Object)
Dim oBlkRef As AcadBlockReference

If Object.ObjectName = "AcDbBlockReference" Then
    Set oBlkRef = Object
    If oBlkRef.Name = "Whatever your block name is" Then
    'or use If oBlkRef.Name Like "SYM*" 'for a wildcard match
        Select Case oBlkRef.XScaleFactor
            Case Is = 48#
                ThisDrawing.Layers.Add "Co-I-SYMB-48"
                oBlkRef.Layer = "Co-I-SYMB-48"
           
            Case Is = 96#
                ThisDrawing.Layers.Add "Co-I-SYMB-96"
                oBlkRef.Layer = "Co-I-SYMB-96"
            'Add all other cases.....
           
        End Select
    End If
End If

End Sub

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
If Layer Exists Then:
« Reply #24 on: February 19, 2005, 09:01:49 PM »
or this to test for all layers
Code: [Select]

Sub test()
Dim varLayers(0 To 5)
Dim booLay(0 To 5) As Boolean
Dim objlay As AcadLayer
Dim intCnt As Integer
Dim strLabel As Variant

varLayers(0) = "lay1": varLayers(1) = "lay2": varLayers(2) = "lay3"
varLayers(3) = "lay4": varLayers(4) = "lay5": varLayers(5) = "lay6"
For Each objlay In ThisDrawing.Layers
 For intCnt = 0 To UBound(varLayers)
   If StrComp(objlay.Name, varLayers(intCnt), vbTextCompare) = 0 Then
     booLay(intCnt) = True
   End If
 Next intCnt
Next objlay

For intCnt = 0 To UBound(booLay)
 If Not booLay(intCnt) Then
   If varLayers(intCnt) Like "lay1" Then
     Call Lay1
   ElseIf varLayers(intCnt) Like "lay2" Then
     Call Lay2
   'and more elseifs to fill it in
   End If
 End If
Next intCnt

End Sub

Sub Lay1()
Dim objlay As AcadLayer
Dim color As New AcadAcCmColor
color.ColorIndex = 5
Set objlay = ThisDrawing.Layers.Add("Lay1")
objlay.TrueColor = color
objlay.Linetype = "CONTINUOUS"
objlay.Lock = True
End Sub

Sub Lay2()
Dim objlay As AcadLayer
Dim color As New AcadAcCmColor
color.ColorIndex = 3
Set objlay = ThisDrawing.Layers.Add("Lay2")
objlay.TrueColor = color
objlay.Linetype = "HIDDEN"
objlay.Lock = True
End Sub

It will error if the linetypes aren't loaded as is.
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)

ML

  • Guest
If Layer Exists Then:
« Reply #25 on: February 21, 2005, 10:48:52 AM »
Hi Jeff

I really like the code you posted for the event procedure Object Added, thank you.

I have been messing around with it for the last hour or so.

It is working very except for this part:
Code: [Select]

oBlkRef.Layer = "Co-I-SYMB-48


The other thing to consider is that the block will be inserted as a non-exploded block but the user will (more then likely) explode it eventually for other reasons.

After the block is exploded, it will then adopt its original layer which was:
Co-I-SYMB

This makes me wonder if my original method of renaming the block from
Co-I-SYMB to Co-I-SYMB-48 if the scale is 48 is a better approach? I do like this method better as well.

I modified your code to this :

Code: [Select]


Private Sub AcadDocument_ObjectAdded(ByVal Object As Object)

Dim oBlkRef As AcadBlockReference
Dim layer As AcadLayer

If Object.ObjectName = "AcDbBlockReference" Then
    Set oBlkRef = Object
    Set layer = ThisDrawing.Layers.Item("CO-I-SYMB")
    If oBlkRef.Name = "Intercomm1" Then
    'or use If oBlkRef.Name Like "SYM*" 'for a wildcard match
        Select Case oBlkRef.XScaleFactor          
            Case Is = 48#, Is = ThisDrawing.Layers.Item("CO-I-SYMB")
            layer.Name = ("CO-I-SYMB-48")

            Case Is = 96#, Is = ThisDrawing.Layers.Item("CO-I-SYMB")
            layer.Name = ("CO-I-SYMB-96")
            'Add all other cases.....
      End Select
    End If
End If


End Sub



Like all of my prior code attemtps, the first inserted block works perfectly but the second inserted block does not.
With the above code, I inserted the Intercomm1 block at 48,
it remaned the layer and all.

After I inserted the second one at 96, It highlighted this line:
Code: [Select]

 Case Is = 48#, Is = ThisDrawing.Layers.Item("CO-I-SYMB")


and said object does not support this method.

Jeff, do you have any idea where the conflict could be?

I think what is happening is that after you insert the second block (scale 96), it still runs through the code that is addressing the scale 48 and there is a conflict.

I wonder if it has to do with the fact that they both share the original layer name CO-I-SYMB?

What do you think?

I hope you understand what I am explaining?

If not, I will clarify

I appreciate the help


Mark

ML

  • Guest
If Layer Exists Then:
« Reply #26 on: February 21, 2005, 11:15:02 AM »
Jeff,

I tried again and here is what I think is happening:

You have a block inserted at 48. Now, you insert the same block at 96.

Well after the module is ran, it is still going to see the block scaled at 48 in the drawing and attempt to create this layer again and that is where the error occurs.

Somehow the module needs to say,
If the block "Intercomm1" is already in the drawing at 48 then ignore it completely and go onto the one you are currently inserting.

I beleve that will solve the problem if only I knew how to address it

I think the event Object aded code that you posted was an attempt to do just that but as I stated in my prior post, the problem still occurs

Thank you again

Mark

ML

  • Guest
If Layer Exists Then:
« Reply #27 on: February 21, 2005, 11:21:06 AM »
Here is yet another one of my attempts.
I think this method is cool because it grabs the xscalefactor from the inserted block and uses it to suffix the layer name

Eve with this method,  the same problem occurs.

Mark

Code: [Select]

Sub LayerSuffixXScaleFactor()


Dim CO_Layers As AcadLayers
Dim Intercomm As AcadBlockReference

Dim BlockXScaleFactor As Double


Set CO_Layers = ThisDrawing.Layers
Set Intercomm = ThisDrawing.ModelSpace.Item(Intercomm1)
 
BlockXScaleFactor = Intercomm.XScaleFactor


If Intercomm.XScaleFactor = 48 Then
 For Each layer In CO_Layers
  If layer.Name = ("CO-I-SYMB") Then
     layer.Name = layer.Name & "-" & BlockXScaleFactor
  End If
 Next
End If

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
If Layer Exists Then:
« Reply #28 on: February 21, 2005, 11:40:42 AM »
Mark, the problem is that once the layer name is changed, it also affects the block definition....IOW, if the block entities are drawn on layer1, then you create the block, then you insert that block and change the layer name of layer1 to layer2, the block definition has no way of keeping layer1 as it's base.......

What you need to do is create a new BLOCK definition with the scale as a part of the name and change the layer of the entities within that block definition.

ML

  • Guest
If Layer Exists Then:
« Reply #29 on: February 21, 2005, 11:52:09 AM »
Hey Jeff,

I am not completely sure I understand what you are refferring to in this part :

Quote

What you need to do is create a new BLOCK definition with the scale as a part of the name and change the layer of the entities within that block definition.


Are you saying go to the base drawing, rename the block, wblock it out, then manipulate it through code?

If this is what you mean, wouldn't I still run into the same problem later?

Thanks
Mark