Jump to content
BrianTFC

How do I center the text in the rectangle and repeat the last input?

Recommended Posts

BrianTFC

Hi All,

 

I'm having a little trouble figuring out how to center my text in the middle of the rectangle after its created. Here's what I have so far, it works but it waits for me to place it. Also I do a lot of panels that are the same size so I also want the lisp to remember the last input so I can continue to use it until I hit escape or return.

If I could get some help I would appreciate it.

 

(defun C:CRP7()
 (setq cmdold (getvar "cmdecho"))
 (setvar "cmdecho" 0)
 (setq PL (getstring "Enter a Panel Label :"))
 (setq PA (getpoint "Select Panel Area :"))
 (command "-boundary" "Advanced" "Island" "No" "Nearest" "" pa "")
 (command "_.change" (entlast) "" "_p" "_la" "Router - Green - V groove" "")
 
 (setq ts(getvar "textsize"))
 (setq tsty(getvar "textstyle"))
 
 (if(= 0 (cdr(assoc 40(tblsearch "style" tsty))))
       (progn
   
 (command "text" "Justify" "Center" pa ts 0 PL)))
 (setvar "cmdecho" cmdold)
    
 (princ)
)

 

Thanks Brian

Share this post


Link to post
Share on other sites
hmsilva

Hi Brian,

 

if you are not using AutoCAD 2015, try the following program.

The 'bpoly' function, is generating an error in AC2015, (not sure why)...

Change the default prompt "123" to a panel size.

(vl-load-com)
(defun c:demo ( / adoc bp cen lays msp pa pl plo reg sfa txt)
 (setq adoc (vla-get-activedocument (vlax-get-acad-object))
       msp (vla-get-modelspace adoc)
       lays (vla-get-layers adoc)
   )
 (vla-add lays "Router - Green - V groove")
 (while (and (setq pa (getpoint "\n Select Panel Area: "))
             (setq pl (getstring (strcat "\nEnter a Panel Label <" (cond (*pl) ("123")) ">: "))
                   *pl (cond ((/= pl "") (strcase pl)) (*pl) ("123"))
                   )
             (setq bp (bpoly pa))
             )
     (setq plo (vlax-ename->vla-object bp)
           sfa (vlax-make-safearray vlax-vbObject '(0 . 0))
     )
     (vla-put-layer plo "Router - Green - V groove")
     (vlax-safearray-put-element sfa 0 plo)
     (setq reg (car (vlax-safearray->list (vlax-variant-value (vla-addregion msp sfa))))
           cen (vlax-get reg 'centroid)
           )
     (vla-delete reg)
     (setq txt (vla-AddText msp *pl (vlax-3D-point (trans cen 1 0)) (getvar 'TEXTSIZE)))
     (vla-put-alignment txt 10)
     (vla-put-textalignmentpoint txt (vlax-3D-point (trans cen 1 0)))
     (vla-put-rotation txt (angle (list 0 0)(getvar 'UCSXDIR)))
   )
 (princ)
 )

Hope that helps

Henrique

Edited by hmsilva
Code revised

Share this post


Link to post
Share on other sites
BIGAL

Perhaps an alternative method if you entlast the bpoly then get 'coordinates then middle of 1st and 3rd is center point.

 

(defun getcoords (ent)
 (vlax-safearray->list
   (vlax-variant-value
     (vlax-get-property
   (vlax-ename->vla-object ent)
   "Coordinates"
     )
   )
 )
)

(defun co-ords2xy ()
; convert now to a list of xy as co-ords are x y x y x y if 3d x y z x y z
(setq numb (/ (length co-ords) 2))
(setq I 0)
(repeat numb
(setq xy (list (nth (+ I 1) co-ords)(nth I co-ords) ))
(setq coordsxy (cons xy coordsxy))
(setq I (+ I 2))
) ; end repeat
)

(setq co-ords (getcoords (entlast)))

Share this post


Link to post
Share on other sites
Tharwat

@ Henrique

 

Don't you think with me that it is better to move the functions to a separate sub-function to get the Activedocument , ModelSpace , Layers collection then Adding a new layer out of the loop of the while function ?

 

Regards :)

Share this post


Link to post
Share on other sites
BIGAL

Like Tharwat you will see in my code 2 defuns because I have 1 in my library lisp for std stuff that I use all the time. My post about civ3d lables opens the Aec database only defun in new code. Same Getval nice dcl for input. Its got to the stage where I need to go through all code and standardise.

Share this post


Link to post
Share on other sites
hmsilva
@ Henrique

Don't you think with me that it is better to move the functions to a separate sub-function to get the Activedocument , ModelSpace , Layers collection then Adding a new layer out of the loop of the while function ?

Regards :)

Hi Tharwat,

 

of course you're right! :)

 

The code at #3 was written with an 'if' function, when posted, I saw that the OP wanted the code in a loop, and I modified it (quick and dirty) to a 'while'...

 

My primary goal was, trying to demonstrate to OP an alternative method, and above all, trying to pass the idea that we should validate the return data from a function, before submit that data to another function/command.

 

Code in message #3 revised.

 

Cheers

Henrique

Share this post


Link to post
Share on other sites
Tharwat

The code at #3 was written with an 'if' function, when posted, I saw that the OP wanted the code in a loop, and I modified it (quick and dirty) to a 'while'...

 

Undoubtedly it's nice :thumbsup:

Share this post


Link to post
Share on other sites
yathishkumar

Hi

 

please find the attached lisp.

 

This is from lee mac.

it's work fine

 

thanks

yathishkumar

Text to center.lsp

Share this post


Link to post
Share on other sites
hmsilva
Undoubtedly it's nice :thumbsup:

Thank you Tharwat! :)

Share this post


Link to post
Share on other sites
BrianTFC

Henrique,

 

I'm using AutoCAD 2015, I just haven't gotten a chance to update my info on here. so how do we fix this?

 

Thanks

Brian

Share this post


Link to post
Share on other sites
hmsilva

I'm using AutoCAD 2015, I just haven't gotten a chance to update my info on here. so how do we fix this?

 

That's bad news!

 

Brian, I don't have AC2015 in this ooolllddd laptop, but I remember some threads on a generated error by 'bpoly' function on AC2015, I don't know if it has been corrected in some SP, or not...

 

The error was an endless loop in 'bpoly', if you have availability to try the code, try the code in a new dwg, and with only one open dwg, in case you have to 'kill the process'.

Tell me if it worked or not.

 

If not, probably we'll have to change the 'bpoly' to a command, and test a valid new 'entlast'...

It should work as expected, in AC2015 also

(vl-load-com)
(defun c:demo ( / adoc bp cen ent-l lays msp pa pl plo reg sfa txt)
 (setq adoc (vla-get-activedocument (vlax-get-acad-object))
       msp (vla-get-modelspace adoc)
       lays (vla-get-layers adoc)
   )
 (vla-add lays "Router - Green - V groove")
 (setq ent-l (entlast))
 (while (and (setq pa (getpoint "\n Select Panel Area: "))
             (setq pl (getstring (strcat "\nEnter a Panel Label <" (cond (*pl) ("123")) ">: "))
                   *pl (cond ((/= pl "") (strcase pl)) (*pl) ("123"))
                   )
             (vl-cmdf "-boundary" "Advanced" "Island" "No" "Nearest" "" pa "")
             (setq bp (entlast))
             (not (eq ent-l bp))
             )
     (setq plo (vlax-ename->vla-object bp)
           sfa (vlax-make-safearray vlax-vbObject '(0 . 0))
     )
     (vla-put-layer plo "Router - Green - V groove")
     (vlax-safearray-put-element sfa 0 plo)
     (setq reg (car (vlax-safearray->list (vlax-variant-value (vla-addregion msp sfa))))
           cen (vlax-get reg 'centroid)
           )
     (vla-delete reg)
     (setq txt (vla-AddText msp *pl (vlax-3D-point (trans cen 1 0)) (getvar 'TEXTSIZE)))
     (vla-put-alignment txt 10)
     (vla-put-textalignmentpoint txt (vlax-3D-point (trans cen 1 0)))
     (vla-put-rotation txt (angle (list 0 0)(getvar 'UCSXDIR)))
     (setq ent-l bp)
   )
 (princ)
 )

 

Hope that helps

Henrique

Edited by hmsilva
add code

Share this post


Link to post
Share on other sites
BrianTFC

Henrique,

 

It runs perfect thanks so much. I changed the same line as you did but I couldn't remember the

(vl-cmdf )

I understand why you use the

(setq bp (entlast))

to get (BP) but what does

(not (eq ent -1 bp)

mean?

 

thanks again to everyone for all your help...:D

Brian

Share this post


Link to post
Share on other sites
Tharwat
but what does
(not (eq ent -1 bp)

mean?

Compare yours with the one Henrique did :roll:

Share this post


Link to post
Share on other sites
hmsilva
Henrique,

 

It runs perfect thanks so much.

...what does

(not (eq ent -1 bp)

mean?

 

thanks again to everyone for all your help...:D

Brian

 

You're welcome, Brian!

 

the

(not (eq ent-l bp))

is for testing a valid new bpoly, just test if not equal to last entity before the 'boundary' command.

 

You didn't test the 'bpoly' function on AC2015, correct?

 

Cheers

Henrique

Share this post


Link to post
Share on other sites
BrianTFC

Ok I would like to change the color of the layer when its being created where in this line would I do that?

 

(vla-add lays "Router - Green - V groove")

 

Brian

Share this post


Link to post
Share on other sites
rkent

I use a simple macro to center text.

^C^C.MTEXT;\J;MC;L;A;1X;

Pick to start and pick the opposite corners of the panel, type in label.

 

Just another option to consider.

Share this post


Link to post
Share on other sites
hmsilva
Ok I would like to change the color of the layer when its being created where in this line would I do that?

(vla-add lays "Router - Green - V groove")

 

 

Ok, second attempt....

 

 

You didn't test the 'bpoly' function on AC2015, correct?

 

 

To change the layer color, try

(setq lay (vla-add lays "Router - Green - V groove")
(vla-put-color lay 3);; for color 3

 

 

Henrique

Share this post


Link to post
Share on other sites
BrianTFC

okay had Dumb a** attack I took out

(not (eq ent-l bp))

and it worked great. now I know what you meant by the test line.

 

thanks

 

Henrique

Share this post


Link to post
Share on other sites
hmsilva

You're welcome, Brian

Glad I could help

 

 

Third attempt....

You didn't test the 'bpoly' function on AC2015, correct?

 

 

Henrique

Share this post


Link to post
Share on other sites
BrianTFC

I tried running the first lisp routine that you posted and it didn't work in AC2015.

Share this post


Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...