Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Assign variable based on the number of characters to be removed

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
kameron1967
537 Views, 12 Replies

Assign variable based on the number of characters to be removed

(defun layun (target_obj / original_layer original_layer_info target_obj_atts new_layer )
	(setq
		original_layer (cdr (assoc 8 (entget target_obj)))
		original_layer_info (tblsearch "layer" original_layer)
		target_obj_atts (entget target_obj)
	);setq

	(if (or	(wcmatch original_layer "*-P123"); REMOVES 4 CHARACTERS
			(wcmatch original_layer "*-P123456"); REMOVES 7 CHARACTERS
			(wcmatch original_layer "*-P123456789"); REMOVES 10 CHARACTERS
	    );or

		(progn
			(setq new_layer (substr original_layer 1 (- (strlen original_layer) ??))) ; NEED TO ASSIGN VARIABLE TO MATCH THE NUMBER OF CHARACTERS TO REMOVE
			(if (not (tblsearch "layer" new_layer))
				(progn
					(entmake ;;;begin to make new layer
						(list
							'(0 . "LAYER")
							'(100 . "AcDbSymbolTableRecord")
							'(100 . "AcDbLayerTableRecord")
							(cons 2 new_layer) ;;; layer name
							(assoc 6 original_layer_info) ;;;re-use original linetype
							'(70 . 0) ;;;flags - use default flags
							(assoc 62 original_layer_info) ;;;re-use original color
						);list
					);entmake
				);progn
			);if
			(entmod (subst (cons 8 new_layer) (assoc 8 target_obj_atts) target_obj_atts))
		);progn
	);if
);defun

 Hi guys,

 

I wonder if you could help me assign a character where ?? is shown so that it matches with the number of characters to remove based on the 3 lines above (see wcmatch lines).  At present, whatever number I replace that ?? with, it removes that number of characters from the layer that I selected (if it meets that WCMATCH criteria).  Another option which may be more difficult is to be able to have the routine look at the object layer and if it contains -N- or -D- or -NEW- or -DEMO- then to remove all the characters that follow these layer modifiers.  This way I can cleanly add another project modifier behind these modifers.  Thanks in advance! Smiley Happy

12 REPLIES 12
Message 2 of 13
hmsilva
in reply to: kameron1967

kameron1967,
perhaps something like

 

(if (or	(wcmatch original_layer "*-P123")
	(wcmatch original_layer "*-P123456")
	(wcmatch original_layer "*-P123456789")
    )					;or

  (progn
    (cond ((wcmatch original_layer "*-P123")
	   (setq num 4)
	  )
	  ((wcmatch original_layer "*-P123456")
	   (setq num 7)
	  )
	  ((wcmatch original_layer "*-P123456789")
	   (setq num 10)
	  )
    )
    (setq new_layer (substr original_layer 1 (- (strlen original_layer) num)))

 

Henrique

EESignature

Message 3 of 13
Kent1Cooper
in reply to: kameron1967


@kameron1967 wrote:
....
(if (or (wcmatch original_layer "*-P123"); REMOVES 4 CHARACTERS (wcmatch original_layer "*-P123456"); REMOVES 7 CHARACTERS (wcmatch original_layer "*-P123456789"); REMOVES 10 CHARACTERS );or (progn (setq new_layer (substr original_layer 1 (- (strlen original_layer) ??))) ; NEED TO ASSIGN VARIABLE TO MATCH THE NUMBER OF CHARACTERS TO REMOVE ....

 Hi guys,

 

I wonder if you could help me assign a character where ?? is shown so that it matches with the number of characters to remove based on the 3 lines above (see wcmatch lines).  At present, whatever number I replace that ?? with, it removes that number of characters from the layer that I selected (if it meets that WCMATCH criteria).  Another option which may be more difficult is to be able to have the routine look at the object layer and if it contains -N- or -D- or -NEW- or -DEMO- then to remove all the characters that follow these layer modifiers.  This way I can cleanly add another project modifier behind these modifers. ....


I think you can do it more simply than that, if your Layer names are constructed consistently in a way similar to your examples.  The (vl-string-position) function has the option to search from the end of the string.  From your character counts, it looks like you want to remove everything past the hyphen.  That function can tell you the position of the last hyphen in the string, regardless of how many characters follow it, so you don't need to know how many characters you need to take off.

 

(setq new_layer

  (substr

    original_layer ; the string

    1 ; start at first character

    (1+ (vl-string-position (ascii "-") original_layer nil T)); search from end

  ); substr

); setq

 

That will put into the new_layer variable the same result [namely, "Mickey-Mouse-"], whether the original_layer variable contains "Mickey-Mouse-P123" or "Mickey-Mouse-P123456" or "Mickey-Mouse-P123456789", without the need to establish different numbers of characters to remove for each of those possibilities.

 

For your -N- -D- -NEW- -DEMO- situations, if those hyphens are always the last ones in the Layer names, the same will work for those.  If not, experiment with (vl-string-search) instead, which, while it can't search from the end, can search for a multi-character sub-string, whereas (vl-string-position) can work only with a single character.  Similarly, if the "-P" part of your first set of possibilities is always the only instance of those two characters that can occur together in Layer names, then (vl-string-search) can find where they are for you without caring how many characters follow them.

Kent Cooper, AIA
Message 4 of 13
kameron1967
in reply to: hmsilva

hmsilva - your code worked great!  Thank you very much for your help! 🙂

Message 5 of 13
kameron1967
in reply to: Kent1Cooper

Kent - I think you read my mind.  Thank  you for the suggestion.  I want to provide more example so that it's clear what I'm trying to get at.

 

 (if (or (wcmatch original_layer "*LAYER1-LAYER2-LAYER3-N-P123"); REMOVES THE CHARACTERS ALL THE WAY TO THE 1ST HYPHEN IN FRONT OF  "-N-"
(wcmatch original_layer "*LAYER1-LAYER2-LAYER3-NEW-P123456"); REMOVES CHARACTERS ALL THE WAY TO THE 1ST HYPHEN IN FRONT OF  "-NEW-"
(wcmatch original_layer "*LAYER1-LAYER2-LAYER3-O-P123"); REMOVES CHARACTERS ALL THE WAY TO THE 1ST HYPHEN IN FRONT OF "-O-"

(wcmatch original_layer "*LAYER1-LAYER2-LAYER3-OLD-P123456"); REMOVES CHARACTERS ALL THE WAY TO THE 1ST HYPHEN IN FRONT OF "-OLD"
     );or

 

I guess I need to use the VL-STRING SEARCH but I need it to look for any of these -N-, -NEW-, -O-, -NEW- combination. 

 

Basically, it looks for the strings in the layer, then removes the characters all the way to the first hyphen in front of these layer modifiers.

 

Thanks in advance.

Message 6 of 13
hmsilva
in reply to: kameron1967

You're welcome, kameron1967
Glad I could help

 

Henrique

EESignature

Message 7 of 13
kameron1967
in reply to: hmsilva

hmsilva, I wonder if you'd like to give the other option a try.  I think yours and Kent's idea could be combined in that it looks for the -N- -NEW- -O- or -OLD- in the layer, then it removes all characters up to the 1st hyphen of those modifiers..

Message 8 of 13
hmsilva
in reply to: kameron1967

kameron1967,
see the the first part of the  Kent1Cooper's post...

 

Henrique

EESignature

Message 9 of 13
Kent1Cooper
in reply to: kameron1967

@kameron1967 wrote:

.... 

 (if (or (wcmatch original_layer "*LAYER1-LAYER2-LAYER3-N-P123"); REMOVES THE CHARACTERS ALL THE WAY TO THE 1ST HYPHEN IN FRONT OF  "-N-"
.... 

I guess I need to use the VL-STRING SEARCH but I need it to look for any of these -N-, -NEW-, -O-, -NEW- combination. 

....


Assuming no such Layer name will contain, for example, "-N-" more than once, this should get you started:

 

Command: (setq test "Marilyn-Monroe-N-123-XYZ")
"Marilyn-Monroe-N-123-XYZ"

Command: (substr test 1 (vl-string-search "-N-" test))
"Marilyn-Monroe"

 

And that result will be the same no matter how much stuff follows the "-N-" part.

 

So, perhaps [untested]:

 

(setq new_layer

  (substr

    original_layer

    1

    (vl-string-search

      (cond

        ((wcmatch original_layer "*-N-*") "-N-")

        ((wcmatch original_layer "*-NEW-*") "-NEW-")

        ((wcmatch original_layer "*-O-*") "-O-")

        ((wcmatch original_layer "*-OLD-*") "-OLD-")

      ); cond

      original_layer

    ); vl-string-search

  ); substr

); setq

Kent Cooper, AIA
Message 10 of 13
kameron1967
in reply to: Kent1Cooper

That's excellent, Kent!  Thank you.  I first attempted to combine the codes which took me a while but it's still not working.  Seeing how you put them together really is enlightening!  🙂

Message 11 of 13
kameron1967
in reply to: kameron1967

Please disregard this one message. Thanks again for your awesome help, Kent and Henrique!

Message 12 of 13
Kent1Cooper
in reply to: kameron1967


@kameron1967 wrote:

That's excellent, Kent!  Thank you. ....


You're welcome.  You can add as many other such Layer-name types as you want, with just additional lines in that (cond) function, e.g.:
,,,,

        ((wcmatch original_layer "*-DEMO-*") "-DEMO-")

....

Kent Cooper, AIA
Message 13 of 13
kameron1967
in reply to: kameron1967

Yes, thanks for the tips, Kent.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost