View Full Version : Error on Small Code Change, Can't Figure It Out...
stusic
2013-05-28, 08:43 PM
Hey All,
I'm trying to make (what seems like) a small change to this code, but I get an error returned when I used the modified version. I'm trying to look at the "model" number and determine which vis state to use. The code I'm beginning with only inserts the block with a pre-defined vis state.
"Error: bad function: #<VLA-OBJECT IAcadBlockReference2 000000003d5c0b78>"
Here's the code that works:
(if (setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(xins:putdynamicproperty tmp "Visibility1" "COPELAND RECIP")
)
When I change the above to this, I get an error.
(cond
(
(and (setq blk "M-GROUP-CLD-XML")
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(or (= (substr (xins:assoc "model" com) 1 2) "2D") ;; Copeland Recip - starts with "2D" or "3D"
(= (substr (xins:assoc "model" com) 1 2) "3D")
)
)
(
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(setq vst "COPELAND RECIP")
(xins:putdynamicproperty tmp vis vst)
)
)
(
(and (setq blk "M-GROUP-CLD-XML")
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(or (= (substr (xins:assoc "model" com) 1 2) "4D") ;; Copeland Recip - starts with "4D" or "6D"
(= (substr (xins:assoc "model" com) 1 2) "6D")
)
)
(
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(setq vst "COPELAND RECIP - VA")
(xins:putdynamicproperty tmp vis vst)
)
)
)
(defun xins:putdynamicproperty (block propname value / p)
(if (setq p (vl-remove-if-not
'(lambda (x) (wcmatch (strcase (vla-get-propertyname x)) (strcase propname)))
(vlax-invoke block 'getdynamicblockproperties)
)
)
(vl-catch-all-apply 'vlax-put (list (car p) 'value value))
)
)
This function returns the model name, which I know works because I use it elsewhere in the code. It returns something like "4DJNR28ML-TSK-C27"
(xins:assoc "model" com)
Even if I change it and replace it with something like this (for testing), I get the same error.
(setq comp "4DJNR28ML-TSK-C27")
Is there any hope for me?
BlackBox
2013-05-28, 08:59 PM
http://www.cadtutor.net/forum/showthread.php?76411-Get-the-visablity-state-of-dynamic-block
stusic
2013-05-29, 01:09 PM
Okay, I read through the linked post (and the one linked inside that), but I see a couple problems: 1.) The code that you posted is a little advanced for me, so it makes it hard to deconstruct it to just set the state. 2.) I tried LM's code, but get the same error when I replace my other subroutine with his (Error: bad function: #<VLA-OBJECT IAcadBlockReference2 00000000388226c8>). This, and what I've listed below, has led me to believe it's not the vis state that messing up, but some other part of the code.
Big hint?
; warning: local variable used as function: TMP
I've found it stems from the OR statement like that below. I replaced "(xins:assoc "model" com)" with (setq comp "4DJNR28ML-TSK-C27") and it gives me a similar error "Error: bad function: "4DJNR28ML-TSK-C27""
(and (setq blk "M-GROUP-CLD-XML")
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(or (= (substr (comp) 1 2) "2D") ;; Copeland Recip - starts with "2D" or "3D"
(= (substr (comp) 1 2) "3D")
)
)
Overall, I don't see why the original code I posted doesn't work. I've got a block name and a visibility state. The first code that works is a simple IF statement: If we insert this block, set it's visibility state to "X". But if I try to change this to a COND statement and add a few simple tests, it fails. I like using the COND statement because it seems easier for me to read and modify (I've got a bunch of conditionals to address). It seems like it should be simple: If a string contains a defined set of characters, set visibility of a block to X...
BlackBox
2013-05-29, 01:34 PM
It's not as simple as it might seem; you have to test that the desired Visibility State is a member of the Allowed Values, etc. first... Backing up for a moment....
If:
(setq comp "4DJNR28ML-TSK-C27")
... Then (you're using a variable, not calling a sub-function):
(and (setq blk "M-GROUP-CLD-XML")
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(or (= (substr comp 1 2) "2D") ;; Copeland Recip - starts with "2D" or "3D"
(= (substr comp 1 2) "3D")
)
)
... Or simpler still:
(wcmatch (substr comp 1 2) "2D,3D")
stusic
2013-05-29, 01:52 PM
It's not as simple as it might seem; you have to test that the desired Visibility State is a member of the Allowed Values, etc. first... Backing up for a moment....
If:
(setq comp "4DJNR28ML-TSK-C27")
... Then (you're using a variable, not calling a sub-function):
(and (setq blk "M-GROUP-CLD-XML")
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(or (= (substr comp 1 2) "2D") ;; Copeland Recip - starts with "2D" or "3D"
(= (substr comp 1 2) "3D")
)
)
... Or simpler still:
(wcmatch (substr comp 1 2) "2D,3D")
I like the wcmatch, it makes sense, but I'm not sure I quite follow the rest. Wouldn't I need a variable (a string) instead of a subfunction to search as part of my IF statement?
EDIT: Also, Lee Mac's code seems to check for AllowedValules in his subroutine:
(defun LM:SetVisibilityState ( block value )
(
(lambda ( name value )
(vl-some
(function
(lambda ( prop )
(if
(and
(eq name (vla-get-propertyname prop))
(member value (mapcar 'strcase (vlax-get prop 'allowedvalues)))
)
(progn
(vla-put-value prop
(vlax-make-variant value (vlax-variant-type (vla-get-value prop)))
)
value
)
)
)
)
(vlax-invoke block 'getdynamicblockproperties)
)
)
(LM:GetVisibilityParameterName block) (strcase value)
)
)
BlackBox
2013-05-29, 01:59 PM
I'm not sure I quite follow the rest. Wouldn't I need a variable (a string) instead of a subfunction to search as part of my IF statement?
You stated that you used Setq to give the comp variable a string value of "4DJNR28ML-TSK-C27"... But in your code above, you call (comp) as if it were a sub-function without any parameters... Simple replace (comp) with comp.
In order for (comp) to return the same value, you would instead have to use:
(defun comp () "4DJNR28ML-TSK-C27")
... Which is not necessary.
stusic
2013-05-29, 03:02 PM
You stated that you used Setq to give the comp variable a string value of "4DJNR28ML-TSK-C27"... But in your code above, you call (comp) as if it were a sub-function without any parameters... Simple replace (comp) with comp.
Okay, I see. Learned something new today. That happened as I was trying to find the error. The original (which I ultimately need to keep) is a function which pulls a model number from a list and was like:
(xins:assoc "model" com)
That function returns something like "4DJNR28ML-TSK-C27", so I was using that to eliminate the possibility of it being the ins:massoc function causing the problem. That's where I messed up.
So, after I've used the wcmatch to make the code a little closer to "proper", and see the subroutine checks for allowed values, and have eliminated the parens around "comp" (or replaced the whole shebang with (xins:assoc "model" com), like its supposed to be). It still craps out. WTF is wrong?! The thing is, I can use:
(if (setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
(xins:putdynamicproperty tmp "Visibility1" "COPELAND RECIP")
)
But I can't seem to add any other conditional statements to change the "COPELAND RECIP" part...
The code that is working is checking to see if a block instance was successfully placed. It is then setting a specific visibility parameter to a desired state.
In your conditional tests you are setting the block name prior to insertion, the actual insertion of the block instance, and whether a "model" starts with a certain string. If all of the tests pass, you are inserting another instance of the block. You are then assigning to a variable the desired parameter state value. And then you are trying to set a dynamic property to that desired state.
Your code does not assign the dynamic property name to the variable name you are using in the code.
Also, each of your branches of your conditional are doing the same thing with the exception of the desired visibility state value. Maybe you should rethink the flow of your decision making in your code to reduce the redundancy.
stusic
2013-05-31, 01:51 PM
Opie, everything you've said makes sense. I've gone through and re-arranged it to try and get the order correct, as well as reduce the redundant code in the conditionals.
Now, within my larger FOREACH statement, I think I've got it right the way you've directed, but I still get:
Error: bad function: #<VLA-OBJECT IAcadBlockReference2 000000004199f7d8>
I think I'm not passing the info correctly to Lee Mac's subroutine to change the vis state, but I'm not sure what to do...
(foreach com (xins:massoc "compressor" (cdr (assoc rck lst)))
;; Set my block name for the entire conditional statement
(setq blk "M-GROUP-CLD-XML")
;; Run the COND statement to verify which type of visibility is required and set the associated variable (vst)
(cond
((wcmatch (substr (xins:assoc "model" com) 1 2) "2D,3D") ;; Copeland Recip - 2D or 3D.
(setq vst "COPELAND RECIP"))
((wcmatch (substr (xins:assoc "model" com) 1 2) "4D,6D") ;; Copeland Recip - 4D or 6D.
(setq vst "COPELAND RECIP - VA"))
)
;; Insert the block
(setq tmp (vla-insertblock spc (vlax-3d-point ins) blk scl scl scl 0.0))
;; Set vis state
(LM:SetVisibilityState ( tmp vst ))
) ;; FOREACH
Btw, the code is looking much better now. :)
It appears you should remove the surrounding parenthesis to the arguments you are supplying to Lee Mac's subroutine. This should work.
(LM:SetVisibilityState tmp vst )
stusic
2013-05-31, 03:24 PM
It appears you should remove the surrounding parenthesis to the arguments you are supplying to Lee Mac's subroutine. This should work.
(LM:SetVisibilityState tmp vst )
Wow, thank you. For hating so many parentheses, I sure do love adding them everywhere... sheesh. :roll:
Seems to be working well now. I sure appreciate everyone's help. Opie, you're explanation really got me going on the right track, by forcing me to step back and look at what I'm doing, instead of getting lost in the depths.
Yea, you need to know what you want to do before you can tell the program to do it. Sometimes, that means breaking your problem down into multiple steps. You then would make sure each step is producing the results you predict. After that, it is just correct syntax for the code.
sven.129574
2014-10-10, 01:01 PM
It appears you should remove the surrounding parenthesis to the arguments you are supplying to Lee Mac's subroutine. This should work.
(LM:SetVisibilityState tmp vst )
Gahh, I was doing the same thing with another of LM's functions. Thank you, Opie, from someone you didn't even know you were helping.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.