setting layer properties problem

Discussion in 'AutoCAD' started by sregister, Oct 19, 2004.

  1. sregister

    sregister Guest

    Does anyone see what is wrong with the following code? It gives me the error "Terminating Routine: ActiveX Server returned an error: Parameter not optional".

    I pass to it a dotted pair list of colors and lineweights.

    (defun i:chlayc2w (lstc2w / item layclctn layci)
    (setq layclctn (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
    (vlax-for item layclctn
    (setq layci (vla-get-ColorIndex (vla-get-TrueColor item)))
    (vlax-put-property item "Lineweight" (cdr (assoc layci lstc2w)))
    (if (= layci 165) (vlax-put-property item "PlotStyleName" "Black 70%"))
    (if (= layci 157) (vlax-put-property item "PlotStyleName" "Black 30%"))
    )
    (princ)
    )
     
    sregister, Oct 19, 2004
    #1
  2. sregister

    Joe Burke Guest

    Post a sample argument list.

    Joe Burke
     
    Joe Burke, Oct 20, 2004
    #2
  3. sregister

    sregister Guest

    Here is a sample of the list I send to this function
    (setq lst
    (list
    (cons '0 acLnWtByBlock)
    (cons '1 acLnWt013)
    (cons '2 acLnWt025)
    (cons '3...)
    )
    )

    I use this list in another function to change object lineweights and it works fine there.
     
    sregister, Oct 20, 2004
    #3
  4. sregister

    Joe Burke Guest

    Seems to me you need to test whether the assoc function is returning a value. If nil
    then the error you mentioned.

    (defun i:chlayc2w (lstc2w / item layclctn layci wt)
    (setq layclctn (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
    (vlax-for item layclctn
    (setq layci (vla-get-ColorIndex (vla-get-TrueColor item)))
    (if (setq wt (cdr (assoc layci lstc2w))) ;added
    (vlax-put-property item "Lineweight" wt)
    )
    (if (= layci 165) (vlax-put-property item "PlotStyleName" "Black 70%"))
    (if (= layci 157) (vlax-put-property item "PlotStyleName" "Black 30%"))
    )
    (princ)
    )

    Joe Burke
     
    Joe Burke, Oct 20, 2004
    #4
  5. sregister

    sregister Guest

    It was returning nil, but now that I added your if statement it doesn't return nil. The (vlax-put-property item "Lineweight" wt) line runs so wt is not nil. That is weird how (cdr (assoc layci lstc2w)) returns nil as the argument to vlax-put-property, but set it to a variable and make the variable the argument and it doesn't return nil.
    Do you know why?

    Thanks for all your help. The program is behaving the way I want it to now.
     
    sregister, Oct 20, 2004
    #5
  6. sregister

    Joe Burke Guest

    Hmm... maybe this is will answer your question.

    (if (assoc layci lstc2w)
    (vlax-put-property item 'Lineweight (cdr (assoc layci lstc2w)))
    (vlax-put-property item 'Lineweight <some default value>) ;else
    )

    rather than:
    (if (setq wt (cdr (assoc layci lstc2w)))
    (vlax-put-property item "Lineweight" wt)
    )


    I used the additional "wt" symbol just so the assoc function didn't need to be
    repeated. Sorry if that was confusing. Generally it's not a good idea to assign a
    value to a symbol which is only used once.

    Also notice the else part of the if statement. The layer was simply skipped (no
    weight assigned) without it. Maybe that's what you want?

    BTW, you should quote the property like this 'Lineweight rather than "Lineweight".
    Why? Because Tony Tanzillo says so. :)

    Joe Burke
     
    Joe Burke, Oct 20, 2004
    #6
  7. sregister

    sregister Guest

    I tried

    (if (assoc layci lstc2w)
    (vlax-put-property item 'Lineweight (cdr (assoc layci lstc2w)))
    )

    but get the error.

    If I first assign (assoc layci lstc2w) to a variable like wt and use (vlax-put-property item 'Lineweight wt) then no errors and the program assigns weights to the layers like I want.

    I may stick with the variable since there are 257 items in the list and I would like the program to only search it once, and also since it doesn't work without using the variable.

    I've seen both ways about using ' or "" for properties. If Tony says it's bette to use ', I'll do so.

    Thanks for all your help.
     
    sregister, Oct 21, 2004
    #7
  8. sregister

    Joe Burke Guest

    Strange, I must be missing something here. See developer help regarding the assoc
    function. "If assoc does not find element as a key in alist, it returns nil."

    So this:
    ;; if assoc returns something other than nil, put/apply (cdr (assoc... to layer
    (if (assoc layci lstc2w)
    (vlax-put-property item 'Lineweight (cdr (assoc layci lstc2w))))

    Should have the same net effect as this:
    ;; if wt is not nil, put/apply wt to layer
    (if (setq wt (cdr (assoc layci lstc2w)))
    (vlax-put-property item 'Lineweight wt))

    BTW, if your association argument list includes all possible integers returned by
    (vla-get-ColorIndex (vla-get-TrueColor item)), then these tests would not be needed.
    Since you were seeing an error, I can only assume that's not the case. On the other
    hand, I'm not very familiar with those functions.

    Whatever works... and glad I could help.
    Joe Burke
     
    Joe Burke, Oct 21, 2004
    #8
  9. sregister

    sregister Guest

    I tested both just now and they do have the same net effect.
    But if I skip the if statement (because I think the list does contain all possible values), then I get an error. And if I skip the if statement but use a variable then it works.

    So here is what works

    (setq wt (cdr (assoc layci lstc2w)))
    (vlax-put-property item 'Lineweight wt)

    also

    (if (assoc layci lstc2w)
    (vlax-put-property item 'Lineweight (cdr (assoc layci lstc2w)))
    )

    also

    (if (setq wt (cdr (assoc layci lstc2w)))
    (vlax-put-property item 'Lineweight wt)
    )

    This doesn't work

    (vlax-put-property item 'Lineweight (cdr (assoc layci lstc2w)))

    Doesn't make sense to me, but at least I have 3 ways of making it work and only 1 that doesn't.
     
    sregister, Oct 21, 2004
    #9
  10. sregister

    Joe Burke Guest

    Just to dot the i's and cross the t's...

    In theory this should not work without an if statement.
    For the same reason the original function failed in the first place.

    Can't know for sure what's going on without a complete arg list as passed.

    Joe Burke
     
    Joe Burke, Oct 23, 2004
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.