| Author |
Message |
T.Willey
Guest
|
Posted:
Thu Jan 27, 2005 11:30 pm Post subject:
How to write (if /= with vb |
|
|
I use lisp mostly, and am looking to increase my knowledge of programming within AutoCad, so this is my first attempt on my own.
How can you write /= (not equal) in vba? I have seen some methods like
If Lay <> Document.ActiveLayer Then
But it doesn't seem to work for me. Any tips and or comments on the code is appreciated as I am still learning.
Thanks in advance.
Tim
Sub ChangeAll2Layer0()
Dim BlkCol As Object
Dim BlkObj As Object
Dim Obj As Object
Dim LayCol As Object
Dim Lay As Object
Set LayCol = ThisDrawing.Layers
For Each Lay In LayCol
Lay.Lock = False
Lay.LayerOn = True
If Lay = Document.ActiveLayer Then
Lay.Freeze = False
End If
Next
Set BlkCol = ThisDrawing.Blocks
For Each BlkObj In BlkCol
For Each Obj In BlkObj
Obj.Layer = 0
Next
Next
AcadApplication.ZoomExtents
ThisDrawing.PurgeAll
End Sub
|
|
| Back to top |
|
 |
Frank Oquendo
Guest
|
Posted:
Thu Jan 27, 2005 11:35 pm Post subject:
Re: How to write (if /= with vb |
|
|
"<>" is indeed "/=" but only for values. You're comparing objects. VB
uses the Is operator for that:
If Not Lay Is Document.ActiveLayer
Have you considered getting a reference to the active layer or its name
before entering the loop? That's more efficient.
--
There are 10 kinds of people: those who understand binary and those who
don't. |
|
| Back to top |
|
 |
VBA
Guest
|
Posted:
Thu Jan 27, 2005 11:51 pm Post subject:
Re: How to write (if /= with vb |
|
|
Here is your code tighten up a bit.
Public Sub ChangeAll2Layer0()
Dim BlkObj As AcadBlock
Dim Obj As AcadObject
Dim Lay As AcadLayer
For Each Lay In ThisDrawing.Layers
Lay.Lock = False
Lay.LayerOn = True
If Lay = ThisDrawing.ActiveLayer Then
Lay.Freeze = False
End If
Next Lay
For Each BlkObj In ThisDrawing.Blocks
For Each Obj In BlkObj
Obj.Layer = 0
Next Obj
Next BlkObj
Application.ZoomExtents
ThisDrawing.PurgeAll
End Sub
"T.Willey" <nospam@address.withheld> wrote in message
news:4234135.1106850636269.JavaMail.jive@jiveforum2.autodesk.com...
| Quote: | I use lisp mostly, and am looking to increase my knowledge of programming
within AutoCad, so this is my first attempt on my own.
How can you write /= (not equal) in vba? I have seen some methods like
If Lay <> Document.ActiveLayer Then
But it doesn't seem to work for me. Any tips and or comments on the code
is appreciated as I am still learning.
Thanks in advance.
Tim
Sub ChangeAll2Layer0()
Dim BlkCol As Object
Dim BlkObj As Object
Dim Obj As Object
Dim LayCol As Object
Dim Lay As Object
Set LayCol = ThisDrawing.Layers
For Each Lay In LayCol
Lay.Lock = False
Lay.LayerOn = True
If Lay = Document.ActiveLayer Then
Lay.Freeze = False
End If
Next
Set BlkCol = ThisDrawing.Blocks
For Each BlkObj In BlkCol
For Each Obj In BlkObj
Obj.Layer = 0
Next
Next
AcadApplication.ZoomExtents
ThisDrawing.PurgeAll
End Sub |
|
|
| Back to top |
|
 |
T.Willey
Guest
|
Posted:
Thu Jan 27, 2005 11:54 pm Post subject:
Re: How to write (if /= with vb |
|
|
No I didn't think of doing that, but that would be no problem to add. Will test now.
Thanks.
Tim |
|
| Back to top |
|
 |
T.Willey
Guest
|
Posted:
Fri Jan 28, 2005 12:19 am Post subject:
Re: How to write (if /= with vb |
|
|
Thanks for the help all.
Now I have a different question. The way it is now working is ok, but it doesn't change the attributes of blocks. How can I write an If and statement? I didn't see anything in the help topics with vba or autocad, and couldn't find anything with a quick search of the ng.
What I'm look for is something like
If And Obj.ObjectName = "AcDbBlockReference" Obj.HasAttributes = True then
I tried it this way also, and it doesn't like it.
If Obj.ObjectName = "AcDbBlockReference" And Obj.HasAttributes = True Then
Then once that is working, to envoke the method to get the attributes is it just
Obj.GetAttributes ??
Here is what I am trying to do, but can't get it to work.
Set BlkCol = ThisDrawing.Blocks
For Each BlkObj In BlkCol
For Each Obj In BlkObj
If Obj.ObjectName = "AcDbBlockReference" And Obj.HasAttributes = True Then
For Each AttObj In Obj.GetAttributes
AttObj.Layer = 0
Next
End If
Obj.Layer = 0
Next
Next
Thanks again.
Tim |
|
| Back to top |
|
 |
Frank Oquendo
Guest
|
Posted:
Fri Jan 28, 2005 12:35 am Post subject:
Re: How to write (if /= with vb |
|
|
T.Willey wrote:
| Quote: | Set BlkCol = ThisDrawing.Blocks
|
This is a collection of block definitions, not block inserts.
Grab all the blocks in your drawing using a filtered selection set. Then
you can step through the set as expected.
--
There are 10 kinds of people: those who understand binary and those who
don't. |
|
| Back to top |
|
 |
T.Willey
Guest
|
Posted:
Fri Jan 28, 2005 12:48 am Post subject:
Re: How to write (if /= with vb |
|
|
I thought that within the block collection the model and paper space blocks resided? If that is true, then wouldn't what I have written search through all model/paper space. Once it comes across a block, then it will test if it has attributes, and if so then it will change them to layer 0 also. That is why I did the double for each call.
Am I totally off base?
Thanks for your help, and patience.
Tim |
|
| Back to top |
|
 |
Frank Oquendo
Guest
|
Posted:
Fri Jan 28, 2005 1:09 am Post subject:
Re: How to write (if /= with vb |
|
|
T.Willey wrote:
| Quote: | I thought that within the block collection the model and paper space blocks resided?
|
Yes but you're looking for attributed block inserts, not blocks with
attribute definitions. That's why "AcDbBlockReference" and HasAttributes
are not working for you.
| Quote: | If that is true, then wouldn't what I have written search through all model/paper space.
|
No. You'd have to iterate the contents of each layout block. Your
current code doesn't delve into the layouts at all. It simply asks if
the layout is a block reference with attributes and moves along when the
answer comes back "No".
| Quote: | Am I totally off base?
|
No, you're just not looking at the right thing. Try this:
Dim fType(1) As Integer, fData(1) As Variant
Dim ss As AcadSelectionSet, blkRef As AcadBlockReference
fType(0) = 0 : fData(0) = "INSERT"
Set ss = new AcadSelectionSet("ss")
ss.Select , , fType, fData
For Each blkRef In ss
If blkRef.HasAttributes Then
'Do your thing
End If
Next
Fair warning: this simplistic approach has a couple of gotchas.
1) You must test for the presence of the selection set or the second run
will crash.
2) HasAttributes returns False if the insert has only constant attributes.
You might consider calling GetAttributes and GetConstantAttributes on
every block in the selection set. Using a For Next loop to iterate the
attribute entities will cause your processing code to be called only if
something is actually returned by those functions, thus alleviating the
need for error trapping.
--
There are 10 kinds of people: those who understand binary and those who
don't. |
|
| Back to top |
|
 |
fantum
Guest
|
Posted:
Fri Jan 28, 2005 1:24 am Post subject:
Re: How to write (if /= with vb |
|
|
| Quote: | If Obj.ObjectName = "AcDbBlockReference" And Obj.HasAttributes = True Then
|
Please note that the VB "And" operator does not short-circuit. If Obj.ObjectName <> "AcDbBlockReference" then it will still try to evaluate Obj.HasAttributes resulting in a run-time error. |
|
| Back to top |
|
 |
T.Willey
Guest
|
Posted:
Fri Jan 28, 2005 1:58 am Post subject:
Re: How to write (if /= with vb |
|
|
For Each AttObj In Obj.GetAttributes
It doesn't like this line now. I guess it is because Obj.GetAttributes returns as a variant. How would I step though a variant?
Thanks again.
Tim |
|
| Back to top |
|
 |
T.Willey
Guest
|
Posted:
Fri Jan 28, 2005 1:58 am Post subject:
Re: How to write (if /= with vb |
|
|
Thanks for letting me know. I guess I'm to used to lisp.
Tim |
|
| Back to top |
|
 |
T.Willey
Guest
|
Posted:
Fri Jan 28, 2005 3:02 am Post subject:
Re: How to write (if /= with vb |
|
|
Here is what I got so far. I found a post that showed one way I'm guessing to work with variants, but when I plugged it in it's not working still.
Tim
Set BlkCol = ThisDrawing.Blocks
For Each BlkObj In BlkCol
If BlkObj.IsLayout = True Then
For Each Obj In BlkObj
If Obj.ObjectName = "AcDbBlockReference" Then
If Obj.HasAttributes = True Then
Set AttObj = Obj.GetAttributes
'For Each AttObj In Obj.GetAttributes
'above didn't work
For i = LBound(AttObj) To UBound(AttObj)
AttObj(i).Layer = 0
'AttObj.Layer = 0
Next
Set AttObj = Obj.GetConstantAttributes
For i = LBound(AttObj) To UBound(AttObj)
AttObj(i).Layer = 0
'For Each AttObj In Obj.GetConstantAttributes
'above didn't work
'AttObj.Layer = 0
Next
End If
End If
Obj.Layer = 0
Next
Else
For Each item In Obj
item.Layer = 0
Next
End If
Next |
|
| Back to top |
|
 |
Frank Oquendo
Guest
|
Posted:
Fri Jan 28, 2005 3:15 am Post subject:
Re: How to write (if /= with vb |
|
|
T.Willey wrote:
| Quote: | Set AttObj = Obj.GetAttributes
|
Drop the 'Set'.
--
There are 10 kinds of people: those who understand binary and those who
don't. |
|
| Back to top |
|
 |
T.Willey
Guest
|
Posted:
Fri Jan 28, 2005 3:19 am Post subject:
Re: How to write (if /= with vb |
|
|
THANK YOU!!!!!
It works now, after all you help, and others.
Now to see how much more trouble I can get in to.
One question, why did I have to take out the set?
Thanks again to all who helped me.
Tim |
|
| Back to top |
|
 |
Laurie Comerford
Guest
|
Posted:
Fri Jan 28, 2005 3:31 am Post subject:
Re: How to write (if /= with vb |
|
|
Hi,
That's a great question. When I started programming in AutoCAD, I had a
rule:
Write a line of code like
a = b
If it won't work, change it to
Set a = b
Inevitably it worked to get my code functioning.
As I gained further experience I found that you use Set when you are
assigning an object, and don't use it when dealing with non-object items
like strings, numbers etc.
--
Regards,
Laurie Comerford
www.cadapps.com.au
"T.Willey" <nospam@address.withheld> wrote in message
news:22960059.1106864401849.JavaMail.jive@jiveforum2.autodesk.com...
| Quote: | THANK YOU!!!!!
It works now, after all you help, and others.
Now to see how much more trouble I can get in to.
One question, why did I have to take out the set?
Thanks again to all who helped me.
Tim |
|
|
| Back to top |
|
 |
|
|
|
|