Jump to content

need help shortening my 'for' statements


Recommended Posts

Posted

 
(defun c:test ( / LayerLocked LayerFrozen)
(vl-load-com)
    (setq
      acadObject       (vlax-get-acad-object)
      acadActiveDocument    (vla-get-ActiveDocument acadObject)
    )
;;;  Modify Layer Properties
 (setq
   acadLayers (vla-get-Layers acadActiveDocument)
 )
 (vlax-for eachLayer acadLayers
   (if
     (= (vla-get-Lock eachLayer) :vlax-true)

   (progn
     (setq LayerLocked (cons eachLayer LayerLocked))
     (vla-put-Lock eachLayer :vlax-false)     
  )
   )
 )

 (vlax-for eachLayer acadLayers
   (if
     (= (vla-get-Freeze eachLayer) :vlax-true)
     (progn
(setq LayerFrozen (cons eachLayer LayerFrozen))
(vla-put-Freeze eachLayer :vlax-false)     
     )
   )
 )
;;;  Restore Layers
(foreach eachLayer LayerLocked
  (vla-put-Lock eachLayer :vlax-true)
)
(foreach eachLayer LayerFrozen
  (vla-put-Freeze eachLayer :vlax-true)
)
(princ)
)

 

 

I need help figuring out how to combine two (vlax-for)

and

the last two (foreach) statments.

 

I have tried (and), (and or) (progn) and every combination until I am so darn confused I can't think straight.

 

No matter my combination it wants to juggle the results.

 

Breaking it down separatly as above works fine but I want to learn how to combine these (for) statements.

 

Thanks for any advise

  • Replies 23
  • Created
  • Last Reply

Top Posters In This Topic

  • cadman6735

    10

  • alanjt

    5

  • Lee Mac

    4

  • jvillarreal

    4

Posted

Hint: You can iterate through the layer collection once by placing both of your conditions in one vlax-for statement.

Also, look at replacing if/progn with and.

 

As for combining your foreach statements, why not use a mapcar statement?

Posted

Hi jvill

 

yes, I am wanting to place both of my conditions into one vlax-for statement but when I do, I lose control of my layers.

 

If I replace the if/progn with and

will this not be looking for both conditions, frozen and locked on one layer?

 

My layers can be a combination of:

 

both thawed and unlocked

one thawed and one locked

one unlocked and one frozen

etc... (any combination)

 

 

If I just use (and) doesn't this mean that the layer has to be both locked and frozen?

Posted

Keep the conditions separated under the same vlax-for statement.

Example:

(and
(this is true)
(do this)
(do this too)
)
(and
(this is true)
(do this)
(do this too)
)

Posted
When you wanna compare your solution, scroll down:




























(vlax-for eachLayer acadLayers
(and
 (= (vla-get-Lock eachLayer) :vlax-true)
 (setq LayerLocked (cons eachLayer LayerLocked))
 (vla-put-Lock eachLayer :vlax-false) 
)
(and
 (= (vla-get-Freeze eachLayer) :vlax-true)
 (setq LayerFrozen (cons eachLayer LayerFrozen))
 (vla-put-Freeze eachLayer :vlax-false) 
)
)

;;;and the mapcar solution:

(mapcar
(function (lambda (locked frozen)
 (vla-put-lock locked :vlax-true)(vla-put-freeze frozen :vlax-true)
))
LayerLocked LayerFrozen
)

Posted

jvill

 

I can't thank you enough for the method you use in helping me. (I come here looking for hints and techniques, not quick easy answers) but sometimes seeing my failed code written correctly after hours of playing with it really helps.

 

I nailed the (vlax-for) (and) (and)... but not without your hit in your second post

 

I failed at getting the mapcar to work... I couldn't figure out the lambda function by the example in help, with what I was trying to do,,, (I couldn't connect the dots)

 

Here is my failed attempt just to show I was getting close

 

 

 
(defun c:test ( / LayerLocked LayerFrozen)
(vl-load-com)
    (setq
      acadObject       (vlax-get-acad-object)
      acadActiveDocument    (vla-get-ActiveDocument acadObject)
    )
;;;  Modify Layer Properties
 (setq
   acadLayers (vla-get-Layers acadActiveDocument)
 )
 (vlax-for eachLayer acadLayers
   (and
     (= (vla-get-Lock eachLayer) :vlax-true)
     (setq LayerLocked (cons eachLayer LayerLocked))
     (vla-put-Lock eachLayer :vlax-false)
   )
   
   (and
     (= (vla-get-Freeze eachLayer) :vlax-true)
     (setq LayerFrozen (cons eachLayer LayerFrozen))
     (vla-put-Freeze eachLayer :vlax-false)
   )
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  Restore Layers
 ;(mapcar
   ;'(lambda ()
  (foreach eachLayer LayerLocked
    (vla-put-Lock eachLayer :vlax-true)
  (foreach eachLayer LayerFrozen
    (vla-put-Freeze eachLayer :vlax-true)
         )
  )
    
 
(princ)
)

 

as you can see I gave up on the mapcar and lambda and just used two foreach statements.

 

seeing your example and then your code really helped me, thank you... I was getting brain tired and frutrated.

Posted

what does this mean?

 

 
(mapcar
(function (lambda (locked frozen)
 (vla-put-lock locked :vlax-true)(vla-put-freeze frozen :vlax-true)
))
[b][i][u][color=red]LayerLocked LayerFrozen   <----
[/color][/u][/i][/b])

 

 

What are you doing there?

Posted
what does this mean?

 

 
(mapcar
(function (lambda (locked frozen)
(vla-put-lock locked :vlax-true)(vla-put-freeze frozen :vlax-true)
))
[b][i][u][color=red]LayerLocked LayerFrozen <----[/color][/u][/i][/b]
)

 

 

What are you doing there?

 

Anytime cadman..i like trying it before being given the answer as well.

I'm passing the lists to mapcar there.

Here's a small example with the mapcar statement.

 

(mapcar
'(lambda (Locked Frozen)
  (do something to Locked)
  (do something to Frozen)
)
(Locked List) (Frozen List)
)

Posted

never mind I figured it out....

 

I tell you, I don't think I would have solved the mapcar lambda on my own...

I am looking at and still don't understand it... I will need to study this...

 

Thanks again

Posted

Just for fun, here's another way...

 

(defun foo (/ lst)
 (vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
   (and (eq (vla-get-lock x) :vlax-true)
        (setq lst (cons (list x 'Lock :vlax-true) lst))
        (vla-put-lock x :vlax-false)
   )
   (and (eq (vla-get-freeze x) :vlax-true)
        (setq lst (cons (list x 'Freeze :vlax-true) lst))
        (vla-put-freeze x :vlax-false)
   )
 )

 ;; DO STUFF

 (foreach x lst (apply (function vlax-put-property) x))
)

Posted

This is what I like to see, different techniques to accomplish the same result. It opens the mind.

 

 

Basicaly

if one layer is in both states (frozen) and (locked), lets say layer 0 is Frozen and Locked, the list "lst" will contain layer "0" twice, one instance being frozen, and one instance being locked.

 

so as "lst" is being passed to the (foreach)function, Layer 0 will pass thru the (foreach) function twice, once to restore the frozen layer and once to restore the locked layer.

 

Is this correct??? (fingers crossed, that I am right)

 

 

Thanks Alan

Posted (edited)

(setq lst (cons (list x 'Freeze :vlax-true) lst))

 

 

Alan

(setq lst (cons (list x 'Freeze :vlax-true) lst))

How does (lst) get the property (‘Freeze) from (x) as each layer passes thru (x) without the (vlax-get-property) function?

Edited by cadman6735
  • 2 weeks later...
Posted

Alan

 

Sorry for the bother but I really do want to understand my question above.

 

Thanks

Posted
Alan

 

Sorry for the bother but I really do want to understand my question above.

 

Thanks

I freeze the layer in the next line of code. The lst variable is just so I can later apply vlax-put-property to each sublist.

 

eg.

(foreach x lst (apply (function vlax-put-property) x))

Posted

Thanks for the reply Alan

 

Sorry, I don't think I am asking my question correctly.

 

 

 
(setq lst (cons (list x 'Lock :vlax-true) lst))

 

Where did 'Lock come from? or

 

 
(setq lst (cons (list x 'Freeze :vlax-true) lst))

 

Where did "Freeze come from?

 

It looks to me you are making a list using some sort of get property technique.

 

 
(setq lst (vlax-get-property Freeze 'ActiveLayer))

Posted

'Lock and 'Freeze are symbol arguments for the vlax-put-property function (hence why they are quoted).

 

Alan could have alternatively used:

 

(setq lst (cons (list x "Lock" :vlax-true) lst))

Following the vlax-for loop through the layer collection, the list 'lst' might look something like:

 

((#<VLA-OBJECT IAcadLayer 0e1d01cc> 'Lock :vlax-true) ... )

# being a VLA Layer Object. Each sublist is a list of arguments for the vlax-put-property function.

 

Then, in the foreach loop at the end of the program, these sublists are supplied as arguments to the apply function:

 

(apply 'vlax-put-property '(<VLA LayerObject> 'Lock :vlax-true))

Which is equivalent to:

 

(vlax-put-property <VLA LayerObject> 'Lock :vlax-true)

Posted
Sorry for jumping in Alan

Oh, no worries. I was just laughing at it. Step in an explain my work anytime. :)

Posted

I think I understand.

 

 
(vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))    [color=red];this gets me to the layer properties[/color]

 

 

(and (eq (vla-get-lock x) :vlax-true)    [color=red];this get me the lock variable [/color]

 

 

(setq lst (cons (list x 'Lock :vlax-true) lst))     [color=red];this creates the list of locked layers[/color]

 

 
(vla-put-freeze x :vlax-false)    [color=red] ;goes thru the vlax-for turning all frozen layers to thawed[/color]

 

 

So, I am going to ask a silly question, 'Freeze and 'Lock are the properties of Layer, 'Freeze and 'Lock are arguments not symbols so I could not use any arbitrary word such as 'Snow, for example.

 

 

'Lock and 'Freeze are symbol arguments for the vlax-put-property function (hence why they are quoted).

 

Why are they quoted? I have been wondering how and when to know to use 'quote and what do they mean. I have seen it in (mapcar) and (lambda)

 

from help menu:

 

 
(mapcar  '(lambda (x) 
         (+ x 3)          )          '(10 20 30))

 

 

'quoted lists and 'quoted function and 'quoted arguments (this I don't understand)

 

 

Anyway guys, thanks for the education

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...