Aftertouch Posted February 1, 2019 Posted February 1, 2019 (edited) Hello all, Ik having some strugles with putting a truecolor of a layer. I have this code: (defun CREATELAYER ( name color linetype lineweight / layercollection newlay ) (setq layercollection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))) (setq newlay (vla-add layercollection name)) (vla-put-color newlay color) (vla-put-linetype newlay linetype) (vla-put-lineweight newlay lineweight) ) Imput: (CREATELAYER "TEST" "2" "CONTINUOUS" "0.18") This works just fine. (Just needs some extra loading functions.) But.. I want to change the imput to: (CREATELAYER "TEST" "132,132,132" "CONTINUOUS" "0.18") Therefor i change the code to: (defun CREATELAYER ( name color linetype lineweight / layercollection newlay ) (setq layercollection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))) (setq newlay (vla-add layercollection name)) (vla-put-truecolor newlay color) (vla-put-linetype newlay linetype) (vla-put-lineweight newlay lineweight) ) But this results in an error. After a bunch of google search, i get a lot of results, but they all seem very 'complex'. I cannot seem to find a simple one-code-line solution for this?? Edited February 1, 2019 by Aftertouch Quote
Grrr Posted February 1, 2019 Posted February 1, 2019 (edited) Hi, There are few reasons for your error: Firstly the color property accepts as argument an integer value within the range of 0-256 (inclusive) - see the reference. So that means with that property you can assign index colors, but not true colors. And with... (defun CREATELAYER ( name color linetype lineweight / ... ) And calling with... (CREATELAYER "TEST" "CONTINUOUS" "2" "0.18") You could see that you provide the value for the linetype into the argument for the color. But even if you switch and pass the "2" for the color value, it will be invalid because its a type of string, so you'll need to pass the integer 2. Since you found out the existance of the truecolor property, you can check its reference and see that it accepts only a color object (AcCmColor). Meaning that the value you were trying to pass "132,132,132" is considered as a type of string.. and again will be an invalid argument. Now that you know what property to use and what type of arguments it accepts the remaining question for you would be "what the heck is an AcCmColor object?" And you can find the answer for it.. just check out the example in the reference, and search in the forum for the vla-SetRGB method. Thats why 3 hours ago, Aftertouch said: After a bunch of google search, i get a lot of results, but they all seem very 'complex'. I cannot seem to find a simple one-code-line solution for this?? Edited February 1, 2019 by Grrr Quote
Aftertouch Posted February 1, 2019 Author Posted February 1, 2019 @Grrr Thanks for the reply. I noticed that i switches the linetype and color in the example. But when i fix that, the vla-put-color seems to accept the value as a string. Ill look into the AcCmColor stuff. Thanks Quote
Lee Mac Posted February 1, 2019 Posted February 1, 2019 (edited) When working with True Colour values, it is usually easier to manipulate the existing AcCmColor object associated with the layer object, e.g.: (defun createlayer ( name color linetype lineweight / lay tru ) (setq lay (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) name)) (if (listp color) (progn (setq tru (vla-get-truecolor lay)) (apply 'vla-setrgb (cons tru color)) (vla-put-truecolor lay tru) ) (vla-put-color lay color) ) (vla-put-linetype lay linetype) (vla-put-lineweight lay lineweight) lay ) Which you can call either with an ACI colour value or list of red, green & blue values: (createlayer "test1" 2 "Continuous" aclnwtbylwdefault) (createlayer "test2" '(100 150 200) "Continuous" aclnwtbylwdefault) Note that this assumes that the supplied linetype is already defined within the active drawing. I would also suggest supplying the Layer Collection as an argument rather than evaluating vlax-get-acad-object for each layer created. Edited February 1, 2019 by Lee Mac 1 Quote
Aftertouch Posted February 1, 2019 Author Posted February 1, 2019 Thanks all, Changed the code to: Accepts both "2" and "132,132,132" as colors in STRING format. Still working on the 'missing linetypes' part. But i can solve that one. (defun CREATELAYER ( name color linetype lineweight / layercollection newlay tcolor ) (setq layercollection (vla-get-layers (HB:ACTIVEDOCUMENT))) (setq newlay (vla-add layercollection name)) (if (< (strlen color) 4) (progn (vla-put-color newlay color) ) (progn (setq color (list (atoi(substr color 1 3))(atoi(substr color 5 3))(atoi(substr color 9 3)))) (setq tcolor (vla-get-truecolor newlay)) (apply 'vla-setrgb (cons tcolor color)) (vla-put-truecolor newlay tcolor) ) ) (vla-put-linetype newlay linetype) (vla-put-lineweight newlay lineweight) ) Quote
ronjonp Posted February 1, 2019 Posted February 1, 2019 (edited) Try this so the linetype won't bomb if it does not exist. (if (tblobjname "ltype" linetype) (vla-put-linetype newlay linetype) ) Why do you want to input the color as a string? What happens if the true color is 1,2,3? Edited February 1, 2019 by ronjonp Quote
Aftertouch Posted February 1, 2019 Author Posted February 1, 2019 I use csv file as a database. Where a layer can have an index or a true color predefined. When reading a csv file, it gives me a string of the index or true color. Worked when i was using regular command calls to create layers. But goong more vla-ish now. You got me on the less numbers thing... working on that. Quote
ronjonp Posted February 1, 2019 Posted February 1, 2019 (edited) 1 hour ago, Aftertouch said: I use csv file as a database. Where a layer can have an index or a true color predefined. When reading a csv file, it gives me a string of the index or true color. Worked when i was using regular command calls to create layers. But goong more vla-ish now. You got me on the less numbers thing... working on that. If possible, format your csv data as columns X Y Z. Then when you parse it, it could be as simple as: (mapcar 'atoi (list "0" "-1" "-2")) Edited February 1, 2019 by ronjonp Quote
Recommended Posts
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.