In need of a lsp

In need of a lsp

NERDASORUS
Advocate Advocate
1,099 Views
11 Replies
Message 1 of 12

In need of a lsp

NERDASORUS
Advocate
Advocate
Hello
Could some one help me with a lsp , a bit of a noob here , promote me to pick a line and it will give me its length in this form (L = ** ) , if some one could show me how to create it would much appreciated.
0 Likes
1,100 Views
11 Replies
Replies (11)
Message 2 of 12

ВeekeeCZ
Consultant
Consultant

One way.

 

(vl-load-com)

(defun c:PrintLength ( / en par)
  (if (and (setq en (car (entsel "\nSelect object: ")))
           (wcmatch (cdr (assoc 0 (entget en))) "*POLYLINE,ARC,CIRCLE,SPLINE,LINE")
           (setq par (vlax-curve-getEndParam en))
           )
    (princ (strcat "L=" (rtos (vlax-curve-getDistAtParam en par) 2 2))))
  (princ))

 

The second and third. 

 

(defun c:PrintLength2 ( / en par)
  (if (and (setq en (car (entsel "\nSelect object: ")))
           (wcmatch (cdr (assoc 0 (entget en))) "*POLYLINE,ARC,CIRCLE,SPLINE,LINE")
           )
    (princ (strcat "L=" (rtos (vla-get-Length (vlax-ename->vla-object en)) 2 2))))
  (princ))


(defun c:PrintLengthofLine3 ( / en p1 p2)
  (if (and (setq en (car (entsel "\nSelect object: ")))
           (= "LINE" (cdr (assoc 0 (entget en))))
           (setq p1 (cdr (assoc 10 (entget en))))
           (setq p2 (cdr (assoc 11 (entget en))))
           )
    (princ (strcat "L=" (rtos (distance p1 p2) 2 2))))
  (princ)
)

 

Better post it in HERE... next time...

 

Message 3 of 12

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

.... 

....
(wcmatch (cdr (assoc 0 (entget en))) "*POLYLINE,ARC,CIRCLE,SPLINE,LINE") ) (princ (strcat "L=" (rtos (vla-get-Length (vlax-ename->vla-object en)) 2 2)))) ....

.... 


The first method is the way to go [and you can add ELLIPSE to the entity types, if you like].  The one quoted above is not -- Circles, Arcs and Splines do not have a Length VLA property.  Circles have a Circumference property, but there is no property that directly holds the length of a Spline or Arc, so it needs to be derived otherwise, such as by the first method..

Kent Cooper, AIA
0 Likes
Message 4 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous.melhem wrote:
... pick a line and it will give me its length in this form (L = ** ) ....

It occurs to me to wonder what you mean, exactly, by "give me its length."  If reporting it at the command line is the intent, @ВeekeeCZ 's first method [for a variety of entity types] or third method [for a Line only] should do, though I would omit the 2's at the ends of the (rtos) functions.  Those will cause it to report only and always in decimal format, to two decimal places.  If you just leave them out, it will report in whatever format and precision are in your current linear Units settings, which I expect would usually be preferred.

 

But if you want [for example] Text drawn with that information as its content, that can also be done.  It can be sent out to a file.  And/or it can be splashed up in the middle of the screen if you prefer it that in-your-face, with an (alert) function.

Kent Cooper, AIA
0 Likes
Message 5 of 12

NERDASORUS
Advocate
Advocate
Thanx all ,what i want is to click a line and a text appears in this form (L = ** ) and thats it 🙂

And im sorry next time ill post it there , didn't know there was a category for it ..

Cheers
0 Likes
Message 6 of 12

ВeekeeCZ
Consultant
Consultant

@Anonymous.melhem wrote:
Thanx all ,what i want is to click a line and a text appears in this form (L = ** ) and thats it 🙂

...

We don't understand what you want. I thought you!'re about to learn stuff.

 

- text in command line?

- text in the middle of selected entity?

- what does mean (L = **)  what about precision...

 

Anyway, try this nice code... originally by Lee Mac, some mods of mine...

 

  (vl-load-com)

;   Lee Mac  ~  29.04.10
(defun c:LengthAtMidpoint ( / *error* spc i ss e Der p obj )

  (defun *error* ( msg )
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ)
  )

  (setq spc (if (or (eq AcModelSpace (vla-get-ActiveSpace (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))))
                    (eq :vlax-true (vla-get-MSpace doc)))
              (vla-get-ModelSpace doc)
              (vla-get-PaperSpace doc)))

  (if (setq i -1
            ss (ssget '((0 . "CIRCLE,ARC,LINE,*POLYLINE"))))
    
    (while (setq e (ssname ss (setq i (1+ i))))
      (setq Der (angle '(0. 0. 0.) (vlax-curve-getFirstDeriv e (vlax-curve-getParamatPoint e (setq p (MidPoint e)))))
            Obj (MCMText spc p 0. (strcat "(L = "
                                          "%<\\AcObjProp Object(%<\\_ObjId "
                                          (GetObjectID (vlax-ename->vla-object e) doc) ">%)."
                                          (cond ((eq "CIRCLE" (setq typ (cdr (assoc 0 (entget e)))))
                                                 "Circumference")
                                                ((eq "ARC" typ)
                                                 "ArcLength")
                                                ("Length"))
                                          " \\f \"%lu6%pr2\">% )")))
      (vla-put-rotation Obj (MakeReadable Der))
      (vla-put-BackgroundFill obj :vlax-true)))
  (princ)
)


(defun MCMText (block point width string / o)
  (vla-put-AttachmentPoint
    (setq o (vla-AddMText block
              (vlax-3D-point point) width string))
    acAttachmentPointMiddleCenter)
  
  (vla-put-InsertionPoint o (vlax-3D-point point))
  
  o)

(defun MakeReadable ( a )
  (cond ((and (> a (/ pi 2)) (<= a pi))
         (- a pi))
        ((and (> a pi) (<= a (/ (* 3 pi) 2)))
         (+ a pi))
        (a)))

(defun GetObjectID ( obj doc )
  (if (eq "X64" (strcase (getenv "PROCESSOR_ARCHITECTURE")))
    (vlax-invoke-method
      (vla-get-Utility doc) 'GetObjectIdString obj :vlax-false)
    (itoa (vla-get-Objectid obj))))

(defun MidPoint ( e )
  (vlax-curve-getPointatDist e (/ (vlax-curve-getDistatParam e
                                    (vlax-curve-getEndParam e))
                                  2.)))

 

Thanks Kent for the corrections.. 

0 Likes
Message 7 of 12

john.uhden
Mentor
Mentor

Remember the olden days?

(command "_.area" "O" ename)

followed by

(getvar "perimeter")

seems to work on circles and ellipses as well as polylines, but not on lines or arcs.

"Hey Joe, what's the area of that straight rebar?"

 

Of course, that reminds of poor old Frank, who was assigned to build an 8' diameter round table out of 2 sheets of plywood.  Someone else laid it out in 8 segments and had drawn the chords to double check the dimensions.  Well, Frank cut along the chords.  I couldn't resist asking him, "Hey Frank, how many sides does a circle have?"  My wise @$$ nephew quickly responded, "Two, of course, ... an inside and an outside."

John F. Uhden

0 Likes
Message 8 of 12

Ranjit_Singh
Advisor
Advisor

Try this

(defun c:somefunc  (/ mtobject mtht)
 (setq mtht     (getreal "\nEnsert mText Height: ")
       mtobject (vla-addmtext (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
                              (vlax-3d-point (getpoint "\nInsert mText Insertion Point :"))
                              0
                              (strcat "L=" "%<\\AcObjProp Object(%<\\_ObjId "
                                      (itoa (vla-get-objectid (vlax-ename->vla-object (car (entsel "\nSelect Polyline: ")))))
                                      ">%).Length \\f \"%lu2%pr2%th44\">%")))
 (vla-put-height mtobject mtht)
 (princ))
0 Likes
Message 9 of 12

NERDASORUS
Advocate
Advocate
Omg !!
What i need is the length/dimension of a line , written in a text in this form "L = " then the value of the dimension . Why would i want it in the command line ?!?! , I dont know how this is confusing that you dont understand it ...

0 Likes
Message 10 of 12

CADaSchtroumpf
Advisor
Advisor

With (grread) for have dynamic text at screen. Move cursor on objects.

 

(defun c:readyn_length ( / AcDoc Space nw_obj ent_text dxf_ent ncol strcatlst Input obj_sel ename)
	(setq
		AcDoc (vla-get-ActiveDocument (vlax-get-acad-object))
		Space
		(if (= 1 (getvar "CVPORT"))
			(vla-get-PaperSpace AcDoc)
			(vla-get-ModelSpace AcDoc)
		)
		nw_obj
		(vla-addMtext Space
			(vlax-3d-point (trans (getvar "VIEWCTR") 1 0))
			0.0
			""
		)
	)
	(mapcar
		'(lambda (pr val)
			(vlax-put nw_obj pr val)
		)
		(list 'AttachmentPoint 'Height 'DrawingDirection 'StyleName 'Layer 'Rotation 'BackgroundFill 'Color)
		(list 1 (/ (getvar "VIEWSIZE") 50.0) 5 (getvar "TEXTSTYLE") (getvar "CLAYER") 0.0 -1 250)
	)
	(setq
		ent_text (entlast)
		dxf_ent (entget ent_text)
		dxf_ent (subst (cons 90 1) (assoc 90 dxf_ent) dxf_ent)
		dxf_ent (subst (cons 63 255) (assoc 63 dxf_ent) dxf_ent)
		ncol 0
	)
	(entmod dxf_ent)
	(while (and (setq Input (grread T 4 2)) (= (car Input) 5))
		(cond
			((setq obj_sel (nentselp (cadr Input)))
				(setq ename (vlax-ename->vla-object (car obj_sel)))
				(foreach typ_measure '("Length" "ArcLength" "Circumference" "Perimeter")
					(if (vlax-property-available-p ename (read typ_measure))
						(setq strcatlst
							(strcat "L =" (rtos (vlax-get-property ename (read typ_measure)) 2 2))
						)
					)
				)
				(if strcatlst
					(mapcar 
						'(lambda (pr val)
							(vlax-put nw_obj pr val)
						)
						(list 'InsertionPoint 'AttachmentPoint 'Height 'DrawingDirection 'StyleName 'Layer 'Rotation 'TextString)
						(list (trans (cadr Input) 1 0) 1 (/ (getvar "VIEWSIZE") 50.0) 5 (getvar "TEXTSTYLE") (getvar "CLAYER") 0.0 (strcat "{\\fArial;" strcatlst "}" )) ;"TechnicBold"
					)
				)
				(setq strcatlst nil)
			)
		)
	)
	(vla-Delete nw_obj)
	(prin1)
)
Message 11 of 12

ВeekeeCZ
Consultant
Consultant

@Anonymous.melhem wrote:
Omg !!
What i need is the length/dimension of a line , written in a text in this form "L = " then the value of the dimension . Why would i want it in the command line ?!?! , I dont know how this is confusing that you dont understand it ...


Don't be so offensive... Maybe you just wanted a quick check tool? I am using many of them... The properties palette is too complex, not clear for a quick look. People have different needs here... often unpredictable. And your description was too vague. 

 

Hope you'll find a proper solution. 

0 Likes
Message 12 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous.melhem wrote:
....
What i need is the length/dimension of a line , written in a text in this form "L = " then the value of the dimension . Why would i want it in the command line ?!?! , I dont know how this is confusing that you dont understand it ...


It's still a question of what you mean by "written."  The command line is where informational reporting is done by commands like LIST and ID, so it makes perfect sense that you might want it there.  Your original question wanted it to "give me its length," which to me sounded exactly like that kind of reporting.

 

To illustrate, load these up.  They're simplified, for LINE entities only for the present, and don't control for whether you actually selected a Line, so you have to do what they ask, but just to demonstrate some possibilities in how the information could be "written":

 

(defun C:RLL-C (/ lin); = Report Line Length - Command line
  (setq lin (car (entsel "\nSelect Line for length report: ")))
  (prompt (strcat "\nL = " (rtos (vlax-curve-getEndParam lin))))
  (princ)
)

(defun C:RLL-A (/ lin); = Report Line Length - Alert
  (setq lin (car (entsel "\nSelect Line for length report: ")))
  (alert (strcat "L = " (rtos (vlax-curve-getEndParam lin))))
  (princ)
)

 

If you use the RLL-C command [I picked the red line with the little blip on it], you get this at the Command line:

 

RLL-C.PNG

 

If you use the RLL-A command on the same Line, you get this out in the middle of the screen:

 

RLL-A.PNG

 

If you mean a Text entity, which was not part of the original question [which may explain why we haven't been thinking along those lines even though the word text has been used in later Posts], then you would have to give some direction on how it should appear [middle-justified on the midpoint of the Line?  above it similar to the look of the Dimensions below?  aligned with the Line's direction or always horizontal?  text size determined by what?  etc., etc.].

 

Or perhaps, since you have now used the word "dimension," you want one of those?  If you make a Dimension Style with "L = " as a prefix, and with both extension lines and both dimension lines suppressed, you can use a Linear or Aligned Dimension and get this, drawn in:

 

RLL-D.PNG

 

That's an actual Dimension entity, with the "L = " part built into it, and unlike Text, it will update its length information content if the Line is Stretched.  And with an Aligned Dimension, it will even follow the direction of a non-orthogonal Line:

 

RLL-Da.PNG

 

and both its informational content and direction will adjust themselves automatically if the Line is adjusted by Stretching:

 

RLL-DaS.PNG

Kent Cooper, AIA