Jump to content
andy81

N/E Labeling with MText improvement

Recommended Posts

andy81

Hello, thank you for taking the time to look at my post.

And thank you all for creating such a useful body of knowledge for beginners like myself to learn from.

 

I have been searching around for a lesson on using MText in AutoLISP

(admittedly i have found a few amazing examples) Lee Mac has posted several replies to similar question. sorry for bringing it up again, but these have been over my head a bit. I am hopping someone will take the time to apply the MText solution to the attached Lisp that i currently do understand. It uses the Text Command...I realize using commands is amateurish, but I am still learning :)

 

; Northing & Easting labeling
; Ryan Anderson December 2013
; The Label will use the current Text Style and current Units Settings
; This Lisp borrows ideas from the tutorials I have been working through.
; http://www.afralisp.net/ http://lee-mac.com/ http://www.cadtutor.net/ http://www.cad-notes.com/


(defun c:gln (/ p x y TxtPos)
(command "_.MSPACE")
(while
(setq p (getpoint "Select a Northing Gridline:"))
(command "_.PSPACE")
(setq TxtPos (getpoint "Pick Label Location: "))
(setq y (rtos (cadr p)))
(setq y (strcat "N " y))
(command "_TEXT" TxtPos "0" y "") ; I would prefer to use MText with a backbround mask and an offset
)
(princ)
)
(princ)

(defun c:gle (/ p x y TxtPos)
(command "_.MSPACE")
(while
(setq p (getpoint "
Select an Easting Gridline:"))
(command "_.PSPACE")
(setq TxtPos (getpoint "
Pick Label Location: "))
(setq x (rtos (car p)))
(setq x (strcat "E " x))
(command "_TEXT" TxtPos "90" x "") ; I would prefer to use MText with a backbround mask and an offset
)
(princ)
)
(princ "Use GLN for Northings, and GLE for Eastings") ;Could both Northings and Eastings be done from one command?
(princ)

 

Cheers!

and thanks again for your time.

Andy.

Share this post


Link to post
Share on other sites
andy81

Ok, I have figured out some of the solution to this. I used Lee Mac's test program

http://www.cadtutor.net/forum/showthread.php?39325-Mtext-in-AutoLISP

(defun c:test (/ ent)
 (if (setq ent (car (entsel "\nSelect MTEXT: ")))
   (foreach x (entget ent)
     (print x)))
 (textscr)
 (princ))

to dump out the Dotted Pairs of MText that suited my needs. Trial and error brought me to understand the MText better when using entmake

my LISP looks like this now.

; Northing & Easting labeling with MText
; Ryan Anderson December 2013
; This Lisp borrows ideas from the tutorials I have been working through.
; http://www.afralisp.net/ http://lee-mac.com/ http://www.cadtutor.net/ http://www.cad-notes.com/


(defun c:gln (/ p x y TxtPos)
(command "_.MSPACE")
(while
(setq p (getpoint "Select a Northing Gridline: "))
(command "_.PSPACE")
(setq TxtPos (getpoint "Pick Label Location: "))
(setq y (rtos (cadr p)))
(setq y (strcat "N " y))
(entmake
 (list
 (cons 0 "MTEXT")
 (cons 5 "549c")
 (cons 100 "AcDbEntity")
 (cons 67 1)
 (cons 410 "Layout1")		        ;Model space or layout tab to place MText on
 (cons 8 "TXT-GEN")			;Layer
 (cons 100 "AcDbMText")
 (cons 10 TxtPos)			;Location of text
 (cons 40 5.0)				;Font Height
; (cons 41 50)				;length of MText Field, if unspecified it will grow or shrink with the input length
 (cons 46 0.0)
 (cons 71 7)	                        ;Text Justification inside MText (1 is top left 7 is bottom left)
 (cons 72 5)
 (cons 1 y)				        ;Text Writen in MText Field
 (cons 7 "Text-03")			;Text Style
 (cons 210 '(0.0 0.0 1.0))
 (cons 11 '(1.0 0.0 0.0))
 (cons 43 5)
 (cons 50 0.0)			        ;Rotation of Text for North Coordinate labels
 (cons 73 1)
 (cons 44 1.0)
 (cons 90 3)			        ;Mask color 3 is "use drawing background color"
 (cons 63 256)				;Turns on background Mask
 (cons 45 1.5)				;Border offset Factor of Background Mask
 (cons 441 0)				;Something to do with background Mask
)
)
(command "_.MSPACE")
)
(princ)
)
(princ)

(defun c:gle (/ p x y TxtPos)
(command "_.MSPACE")
(while
(setq p (getpoint "Select an Easting Gridline: "))
(command "_.PSPACE")
(setq TxtPos (getpoint "Pick Label Location: "))
(setq x (rtos (car p)))
(setq x (strcat "E " x))
(entmake
 (list
 (cons 0 "MTEXT")
 (cons 5 "549c")
 (cons 100 "AcDbEntity")
 (cons 67 1)
 (cons 410 "Layout1")		        ;Model space or layout tab to place MText on
 (cons 8 "TXT-GEN")			;Layer
 (cons 100 "AcDbMText")
 (cons 10 TxtPos)			;Location of text
 (cons 40 5.0)				;Font Height
; (cons 41 50)				;length of MText Field, if unspecified it will grow or shrink with the input length
 (cons 46 0.0)
 (cons 71 7)				;Text Justification inside MText (1 is top left 7 is bottom left)
 (cons 72 5)
 (cons 1 x)					;Text Writen in MText Field
 (cons 7 "Text-03")			;Text Style
 (cons 210 '(0.0 0.0 1.0))
 (cons 11 '(1.0 0.0 0.0))
 (cons 43 5)
 (cons 50 1.5708)			;Rotation of Text for East Coordinate labels
 (cons 73 1)
 (cons 44 1.0)
 (cons 90 3)				;Mask color 3 is "use drawing background color"
 (cons 63 256)				;Turns on background Mask
 (cons 45 1.5)				;Border offset Factor of Background Mask
 (cons 441 0)				;Something to do with background Mask
)
)
(command "_.MSPACE")
)
(princ)
)
(princ "Use GLN for Northings, and GLE for Eastings") ;Could both Northings and Eastings be done from one command?
(princ)

One last problem I am having. I hope someone can help me out. The location of the label is stored in the variable TxtPos. What i want is to be able to offset the text so it does not sit directly on the grid line I have selected. something like adding 5mm to the Y value of TxtPos for Northings and 5mm to the X Value for Eastings.

 

Any help would be appreciated, if there was a simpler way of doing all this, please do not hesitate to correct me.

lastly, if the code is useful for anyone please feel free to use it.

cheers!

Andy.

Share this post


Link to post
Share on other sites
Hippe013

(defun c:gln (/ p x y TxtPos)
(command "_.MSPACE")
(while
(setq p (getpoint "Select a Northing Gridline: "))
(command "_.PSPACE")
(setq TxtPos (getpoint "Pick Label Location: "))

 

If I have this correct you want to pick a point in Model Space retrieve the Northing and Easting and then toggle over to an active layout and place the text.

 

May I offer a different approach to toggling between model and layout?

 

(vl-load-com);Load Visual Lisp Extensions
(setq *acad* (vlax-get-acad-object));Get the ACAD Object
(setq *ad* (vla-get-ActiveDocument *acad*));Get the Active Document
(vlax-put-property *ad* 'ActiveSpace 1);For ModelSpace
(vlax-put-property *ad* 'ActiveSpace 0);For Paperspace
(vlax-release-object *acad*);release object when done using it
(vlax-release-object *ad*);release object when done using it

 

I would even recommend using the objects for creating your MTEXT.

Share this post


Link to post
Share on other sites
andy81

Thanks for your reply Hippe013.

to be clear. I do want to pick a point in Model Space and retrieve the Northing and Easting, but I am doing this through a viewport located on Layout1.

 

Sorry that was not documented at all. (Hopefully this will help others who read this post)

 

The Drawing, with it's Drawing boarder is sitting on Layout 1

one or more Viewports exist in the paperspace (Layout1) looking into Model space.

All Annotations are required to be in Paper space.

All Gridlines Exist in Model Space.

 

So the work flow is:

-Type in Either GLE (Eastings) or GLN (Northings)

-Lisp will switch to model space in the viewport

-prompt to pick a Gridline

-Lisp will switch back to paper Space in Viewport

-prompt to pick a label location

-use the prefix of "E " for Eastings or "N " for Northings

-create a MText at the picked point in Paper space

-something like "E 1500.000" or "N 2400.000"

-Easting Text is Rotated to be vertical

-A background mask the same as the background color is applied

 

Thanks for the code on Visual Lisp Extensions.

I do not exactly follow at the moment, but I will dig deeper into it.

If you have time (or if someone else wants to help out) i'd like to know what MText would look like in this format.

 

cheers,

Andy.

Share this post


Link to post
Share on other sites
Hippe013

Maybe try this? I hope that it helps with understanding VLA objects.

 


(vl-load-com);Load Visual Lisp Extensions

(defun c:glne ()
 
 (setq *acad* (vlax-get-acad-object));Get the acad object

 (setq *ad* (vlax-get-property *acad* 'ActiveDocument));Get the Active Document

 (setq *PS* (vlax-get-property *ad* 'PaperSpace));Get the Active Paper Space

 (vlax-put-property *ad* 'ActiveSpace 1);Go to Model Space

 (setq pnt (getpoint "\Select Point on Grid: "));Prompt User to Select Point:

 (if pnt
   (progn ;If Point Exists

     (vlax-put-property *ad* 'ActiveSpace 0);Go to Paper Space
     
     (setq x (car pnt));Get the X value of Point
     
     (setq y (cadr pnt));Get the Y Value of Point

     (initget 1 "Northing Easting");Initialize getkword

     (setq ret (getkword "Label Northing or Easting?"));Ask user if they are labeling Northing or Easting

     (cond
((= ret "Northing")(setq str (strcat "N:" (rtos y 2 2)) rot 0))
((= ret "Easting")(setq str (strcat "E:" (rtos x 2 2)) rot (/ pi 2.0))));Format the string accordingly and Set the Roation

     (setq txtpos (getpoint "\nSelect Label Position:"));Prompt User for Label Position

     (if txtpos
(progn ;If Point Exists

  (setq MTEXT-OBJECT (vlax-invoke-method *PS* 'AddMText (vlax-3d-point txtpos) 1 str));Create the MTEXT Object

  (vlax-put-property MTEXT-OBJECT 'Layer "TXT-GEN");Set the layer for the MText

  (vlax-put-property MTEXT-OBJECT 'Height 0.12);Set the Text Height

  (vlax-put-property MTEXT-OBJECT 'BackgroundFill :vlax-true);Set the BackgroundFill to true

  (vlax-put-property MTEXT-OBJECT 'AttachmentPoint acAttachmentPointBottomLeft);Set the Jusstification

  (vlax-put-property MTEXT-OBJECT 'InsertionPoint (vlax-3d-Point txtpos));Reset the InsertionPoint

  (vlax-put-property MTEXT-OBJECT 'Rotation rot);Set the rotation
  
  (vlax-release-object *acad*)
  (vlax-release-object *ad*)
  (vlax-release-object *PS*)
  (vlax-release-object MTEXT-OBJECT)
  ;Not sure if its necessary to release every object or just *acad*
  ;either way... it doesn't hurt to just release it
  
  );end progn

;if Point doesn't exist

);end if

     );end progn

   ;if Point doesn't exist

   );end if

 );end defun

 

Share this post


Link to post
Share on other sites
Hippe013

This will help in looking at entities as objects.

 

(vl-load-com)
(defun c:dmpobj ()
 (vlax-dump-object (vlax-ename->vla-object (car (entsel "\nSelect Object:"))) T)
 (command "TextScr")
 (princ)
 )

Share this post


Link to post
Share on other sites
BIGAL

A couple of suggestions

 

; use POLAR to work out a new insert pt for the text
(setq txtpos (polar (polar P 5.0 0.0) -5.0 1.5707963)

(setq y (rtos (cadr p)))
(setq y (rtos (cadr p) 2 0)) ; this is integer
(setq y (rtos (cadr p)1 1)) ; this is 1 decimal place


Share this post


Link to post
Share on other sites
andy81

Thanks Hippe013 and BIGAL

these code snippets look like they will be very helpful.

 

when i get a little more time later in the week i will try to use the VLA method to solve this probelm again. with the offets this time :)

cheers guys!

Andy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×