Jump to content

find midpoint btw 2 user points - then place user defined text at this midpoint


BudPerry

Recommended Posts

I need to create a routine that will allow a user to pick two points (via intersections) and then it will automatically place text (user types in) at the midpoint between these first two points.

 

So far I have:

 

 

 
(defun c:textplacer ()
(command "_textstyle" "standard")
(command "_textsize" "6.0")
(command "_osnap" "Intersection")
(setq pt1 (getpoint "\nSelect First Point:"));gets the first user point
(setq pt2 (getpoint "\nSelect Second Point in POG Direction:"));gets the second user point
(setq pt3 (abs (- (car pt1)(car pt2))));gets the middle point btw pt1 and pt2 

;need the rest here

);end function

 

Not even sure if pt3 is really getting the midpoint...I'm out of my depths here.

Edited by BudPerry
Link to comment
Share on other sites

If you want to use the above AutoLISP routine, then:

(setq pt3 (list (/ (- (car  pt2) (car  pt1)) 2)
               (/ (- (cadr pt2) (cadr pt1)) 2)))

 

And, by the way, please edit your post and add code tags.

Link to comment
Share on other sites

Thank you very much. I hadn't found the M2P command before.

However, I think I'll probably go with your first code suggestion because I also have to have a block inserted at the first and second points as well.

 

Thanks Again!

Link to comment
Share on other sites

You're welcome! And thank you for fixing the first post.

If you encounter furter issues with your code, just post it here and someone will take a look on it.

Link to comment
Share on other sites

I have added this code and it seems to be placing text along the x axis near 0,0 and not actually in between pt1 and pt2... and I'm not smart enough to figure out why!

 

(setq pt1 (getpoint "\nSelect First Point:"));gets the first user point
(setq pt2 (getpoint "\nSelect Second Point in POG Direction:"));gets the second user point
(setq pt3 (list (/ (- (car  pt2) (car  pt1)) 2)
               (/ (- (cadr pt2) (cadr pt1)) 2)));gets middle point
(command "_text" "j" "tc" pt3 "6.0" "0.0" "test");supposed to place text at this middle point

Link to comment
Share on other sites

I have added this code and it seems to be placing text along the x axis near 0,0 and not actually in between pt1 and pt2... and I'm not smart enough to figure out why!

 

Hi Bud,

 

The error in placement is because points supplied to commands are affected by the Object Snap settings.

 

To avoid this, you can use the "_non" (none) Object Snap modifier to ignore the Object Snap settings.

 

Consider the following code:

 

(defun c:test ( /  p1 p2 )
   (if
       (and
           (setq p1 (getpoint "\nSelect First Point: "))
           (setq p2 (getpoint "\nSelect Second Point in POG Direction: " p1))
       )
       (progn
           (command "_.text" "_J" "_TC" "_non" (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) p1 p2))
           (if (zerop (cdr (assoc 40 (tblsearch "STYLE" (getvar 'textstyle)))))
               (command 6.0 0.0 "test")
               (command 0.0 "test")
           )
       )
   )
   (princ)
)

In the above code, I have also allowed for null user input, shortened the method for calculating the midpoint, and have accounted for Text Styles with a predefined Text Height for which there is no height prompt.

 

However, I would also recommend that you consider using the entmake function for this task:

 

(defun c:test ( / p1 p2 p3 )
   (if
       (and
           (setq p1 (getpoint "\nSelect First Point: "))
           (setq p2 (getpoint "\nSelect Second Point in POG Direction: " p1))
       )
       (progn
           (setq p3 (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) p1 p2))
           (entmake
               (list
                  '(0 . "TEXT")
                   (cons 10 p3)
                   (cons 11 p3)
                  '(72 . 1)
                  '(73 . 3)
                  '(40 . 6.0)
                  '(1 . "test")
                   (cons 7 (getvar 'textstyle))
               )
           )
       )
   )
   (princ)
)

Using entmake is much faster than command and has none of the prompts (hence no need to alter CMDECHO), also, entmake is not affected by Object Snap and all of the Text properties may be set through the supplied DXF list. Note however, that the above example does not account for changes in the UCS settings.

 

A reference for the DXF values that I have used can be found here.

 

I hope this helps,

 

Lee

Link to comment
Share on other sites

Wow, that was fast, and it works! Thank you very much. I've never used the entmake function but I can see how it could be much more useful, or at least cleaner.

 

As I continue with this program I may come back and ask for a little more stuff, but I'm going to try to do as much on my own as possible so I can learn.

 

Thanks again!

Link to comment
Share on other sites

You're very welcome Bud :thumbsup:

 

Apart from being significantly faster, entmake / entmakex has several advantages over using a command call (some of which I have already mentioned, but will re-iterate for completeness):

 

  • entmake(x) is a lot faster than using a command call, since this function is directly modifying the drawing database.

 

  • Programs are far more reliable across several AutoCAD versions, as entmake(x) does not rely on the prompt order of a command, which may be subject to change in a new version of AutoCAD.

 

  • entmake(x) is not affected by Object Snap, so no need to alter the OSMODE System Variable which consequently necessitates the use of an *error* handler to reset the Object Snap settings should the program encounter an error.

 

  • entmake(x) does not echo any prompts, so there is no need to alter the CMDECHO System Variable.

 

  • All properties of an entity can be set or modified in a single call to entmake(x) or entmod. The same procedure would require setting each property individually in Visual LISP ActiveX, or using multiple commands.

Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...