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

ASSOC with multiple solutions

5 REPLIES 5
Reply
Message 1 of 6
hdixon
514 Views, 5 Replies

ASSOC with multiple solutions

Hi,

I am wanting to use autolisp to access the text in a MTEXT entity.  assoc(1) gives me the first bit of text, but not the rest.

Is there a way to use assoc to access each of a number of matches or do I need to write my own routine to find all the 1's?

- Or is there some other command I can use?

 

With thanks

5 REPLIES 5
Message 2 of 6
pbejse
in reply to: hdixon

 


@hdixon wrote:

Hi,

I am wanting to use autolisp to access the text in a MTEXT entity.  assoc(1) gives me the first bit of text, but not the rest.

Is there a way to use assoc to access each of a number of matches or do I need to write my own routine to find all the 1's?

- Or is there some other command I can use?

 

With thanks


 

(vla-get-textstring (vlax-ename->vla-object (car (entsel)))) 

 

That will give you the entire String value of an Mtext

 

Sample code

(defun c:Demo ( / mtexts i)
(vl-load-com)			
(if (setq mtexts (ssget '((0 . "MTEXT"))))
			(repeat (setq i (sslength mtexts))
								(print (vla-get-textstring
														 (vlax-ename->vla-object
																	 (ssname mtexts (Setq i (1- i))))))))
	(princ))

 

 

Holler if you need help

  

Message 3 of 6
_gile
in reply to: hdixon

Hi,

 

There's none built-in functions but you'll find many 'massoc' routines which return a list of all entries for the specified key in an association list.

Here's mine:

(defun gc:massoc (code alst)
  (if (setq alst (member (assoc code alst) alst))
    (cons (cdar alst) (gc:massoc code (cdr alst)))
  )
)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 6
pbejse
in reply to: _gile


@_gile wrote:

Hi,

 

There's none built-in functions but you'll find many 'massoc' routines which return a list of all entries for the specified key in an association list.

 

Here's mine:

(defun gc:massoc (code alst)
  (if (setq alst (member (assoc code alst) alst))
    (cons (cdar alst) (gc:massoc code (cdr alst)))
  )
)

 


Nice recursion code _gile,  

 

OP:

in this case its DXF 3 and not 1

 

 

Another, to include 1 & 3

 

(Defun another (code alst)(vl-load-com)
      (mapcar 'cdr
              (vl-remove-if-not
                    (function (lambda (k)
                           (member (car k) alst))
                           ) ;_ end of lambda
                    code
                    ) ;_ end of vl-remove-if-not
              ) ;_ end of mapcar
      ) ;_ end of Defun

 

(ANOTHER data  '(1 3))

 

HTH

Message 5 of 6
hdixon
in reply to: pbejse

Hi,

Thankyou all for your suggestions.  As often happens I have been moved to a different crisis, but expect to be back on this in about a fortnight.

Thanks again for the ideas and suggestions.

Message 6 of 6
Kent1Cooper
in reply to: hdixon


@hdixon wrote:

Hi,

I am wanting to use autolisp to access the text in a MTEXT entity.  assoc(1) gives me the first bit of text, but not the rest.

Is there a way to use assoc to access each of a number of matches or do I need to write my own routine to find all the 1's?

- Or is there some other command I can use?

 

With thanks


This will not just get the assorted sub-string values from the (assoc 3) and (assoc 1) entries for a selected Mtext entity, but put them back together into a single text string:

 

(apply
  'strcat
  (mapcar 'cdr
    (vl-remove-if-not
      '(lambda (x)
        (member (car x) '(3 1))
      ); lambda
      (entget (car (entsel))); [or feed in variable with entity data list if you have it already]
    ); -remove-
  ); mapcar
); apply

 

However, you can get the same result with a lot less code from pbejse's (vla-get-textstring...) approach.

Kent Cooper, AIA

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

Post to forums  

Autodesk Design & Make Report

”Boost