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

Interesting Attribute problem, fixing empty values in refs

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
545 Views, 7 Replies

Interesting Attribute problem, fixing empty values in refs

 

Hey all, been busy with client work for the last few weeks but i've still been tinkering with code when I can.

Recently we've made a change to remove spaces from our instrument bubble attribute defaults.

For whatever reason (perhaps an indent, or perhaps an extra tab, or software issues) there are leading spaces in each of the 4 attribute VALUES that i've removed from the new bubbles coming in, but i'm trying to find the best way to go back and remove all of those spaces from the instrument bubbles that are already within dwgs.

Simply doing a (command "insert" "ce001=") followed by an attsync does not work, unfortunately.

Here , let me show the problem.attspace.jpg

So quite simply I just wish to remove those leading spaces (that you can tell are there by the blue bar in the value field, that's from spaces, as if it's been selected by ctrl+a from within the attribute editor. A count of left-arrow move-overs tells me that there are 6 spaces in these fields, but the code should be able to handle a variable amount of spaces.....in fact i think telling it an amount of spaces as in (eq "      " attval) probably is not the best way, only problem is i need someone to show me what the better method would be 🙂

(vl-load-com)
(if (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 "ce001"))))
    (progn
    (setq ind (sslength ss))
(repeat ind
(setq ind (1- ind)
      ent (ssname ss ind)
      obj (vlax-ename->vla-object ent)
      attval (mapcar 
            '(lambda (x) (vla-get-TextString x))
 (vlax-invoke (vlax-ename->vla-object ent) 'GetAttributes)))

This is as far as I've gotten, and it returns the attval variable as the following, on my sample block shown in the image above:

("FE" "" "      " "      ")

 so i've got a list with all of the values assigned to a var. Just need to know the best way to remove spaces. I've looked into Lee Mac's functions but just nothing seems to work with varying amounts of empty strings (well, strings of only spaces). The logic would be....

If an element contained within the list of attribute values does not have an actual letter/number text value and is only spaces, then to replace that value with an empty string i.e. "", just as if it's been created but has not had any information put into it, INCLUDING SPACES.

I've tried varying methods such as...

(if (eq "      " att)(lm:vl-setattributevalue obj (vla-get-tagstring att)))
(if (eq "      " (nth 1 attval))(lm:vl-setattributevalue obj "TAG10" ""))
(if (eq "      " (nth 2 attval))(lm:vl-setattributevalue obj "TAG20" ""))
(if (eq "      " (nth 3 attval))(lm:vl-setattributevalue obj "TAG30" ""))

that just aren't working too well and rather than bang my head i thought i'd just ask for clues.

 

Thanks!

7 REPLIES 7
Message 2 of 8
Gary_J_Orr
in reply to: Anonymous

Vl-String trim removes all leading and or trailing characters of a given string. To remove spaces (and I'll even throw in tabs JIC):

(vl-string-trim " \t" " ")
(setq yourstringvar (vl-string-trim " \t" yourstringvar))

Happy "lisping"
Gary
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 3 of 8
3wood
in reply to: Anonymous

Find and Replace may do the job quickly.
Message 4 of 8
Anonymous
in reply to: 3wood

True, but I was hoping to just run a lisp by clicking a button each time I incidentally go into the dwgs, this isn't a huge priority.

 

Q:

Once i have the values (vl-trimmed),

how do I actually set them?

 

(defun test  (/ )
(vlax-for Obj
(vla-item
(vla-get-blocks
(vla-get-ActiveDocument
(vlax-get-acad-object))) "ce001")
(setq lst (cons Obj lst))) lst)
(foreach att lst
(setq attd (vl-string-trim " \t" att))(vla-put-textstring att attd))

 

returns nil and doesn't change the attributes on block ce001 😞

 

Message 5 of 8
Anonymous
in reply to: Anonymous

I think I'm just doing it incorrectly.

Message 6 of 8
Gary_J_Orr
in reply to: Anonymous

bhull,

You posted different attempts... your original post that searched for a specific block name, thenyou were trying to get all blocks (but you went the wrong way with that one by searching the blocks table...)

 

Here is what you are trying to get at I believe:

 

(vl-load-com)

(defun RemoveSpacesFromAttributes (BlockName / ss ind ent obj AttbCol Textstr)
  (if (= nil BlockName)
    (setq ss (ssget "_X" (list (cons 0 "INSERT"))))
    (setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 BlockName))))
    )
  (setq ind (sslength ss))
  (if (> ind 0)
    (repeat ind
      (setq ind (1- ind))
      (setq ent (ssname ss ind))
      (setq obj (vlax-ename->vla-object ent))
      (setq AttbCol nil)
      (if (= :vlax-true (vla-get-hasattributes obj))
	(progn
	  (setq AttbCol (vlax-safearray->list
			  (vlax-variant-value
			    (vla-getattributes obj)))
		)
	  (foreach AttObj AttbCol
	    (setq Textstr (vla-get-textstring AttObj))
	    (setq Textstr (vl-string-trim " \t" Textstr))
	    (vla-put-textstring AttObj Textstr)
	    );foreach
	  );progn
	);if has attributes
      (vla-update obj)
      );repeat
    )
  );defun

 

 

With this function you can call it two different ways:

process all blocks (inserts)

(RemoveSpacesFromAttributes nil)

process specific blocks (inserts)

(RemoveSpacesFromAttributes "BlockName")

 

If you will notice, I spread all of the functions out into a "line-by-line" approach. This allows me to work out the issues one at a time. AFTER I get the function to work, then I'll start in on Lambda and mapcar and apply to optimize the function if need be for speed or whatever other concerns.

 

The "(setq AttbCol...)" call was actually created one line at a time while I was building the function so I could inspect each return value for what was needed on the next call but I ended up combining them in this solution, shame on me.... but I left the rest so you could see how it steps into the required subfunctions.

 

Happy "Lisping"

Gary

 

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 7 of 8
Anonymous
in reply to: Gary_J_Orr

Thank you very much.

I had pieces of it correct but wasn't able to pull it together.

Much appreciated!

Message 8 of 8
Gary_J_Orr
in reply to: Anonymous

No prob... and actually, the call to:
(vla-update obj)
is in the wrong place... it should immediately follow the "foreach" call within the progn for the true condition of "has attributes":



);foreach
(vla-update obj)
);progn
);if has attributes

as I posted it, it will update every block that it processes regardless of it finding any attributes within it.
G
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)

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

Post to forums  

Autodesk Design & Make Report

”Boost