Author Topic: Adjust Z coordinates of lines, based on leader and text  (Read 1759 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Adjust Z coordinates of lines, based on leader and text
« on: August 10, 2010, 05:05:44 PM »
Ok, we have drawings with leaders and text pointing to various lines, these pieces of text represent the Z coordinates of the line work that the leaders are touching.

I know a lot of people here are far more skilled than I am with LISP and I was wondering if anyone has a routine that will change the Z elevations of the end of the line that the leader is touching to what the text indicates automatically? I am working on a routine to do this, but figured if someone had already done it, why reinvent the wheel?

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Adjust Z coordinates of lines, based on leader and text
« Reply #1 on: August 10, 2010, 05:16:44 PM »
Easiest way to code it would be prompting for each selection of line and leader text; intelligent way (and possibly inaccurate way) to code it would be to detect either an intersection between leader and line, or closest leader to the line and do them all in one go.

I'm for the former.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adjust Z coordinates of lines, based on leader and text
« Reply #2 on: August 10, 2010, 05:17:55 PM »
So am I, which is what I am working on; however, I was just wondering if someone had already done something like this.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Adjust Z coordinates of lines, based on leader and text
« Reply #3 on: August 10, 2010, 05:30:07 PM »
Quickly code this, but try to do it yourself before you scroll for the solution  ;-)


Code: [Select]
< Scroll for solution... no peeking before you have tried it yourself ;) >































(defun c:lz ( / ln tx l dx10 dx11 k str z )
  ;; Lee Mac 2010

  (while
    (and
      (setq ln (LM:SelectifFoo (lambda ( y ) (eq "LINE" (cdr (assoc 0 (entget y))))) "\nSelect Line: "))
      (setq tx (LM:SelectifFoo (lambda ( y ) (eq "TEXT" (cdr (assoc 0 (entget y))))) "\nSelect Text: "))
    )
    (setq l (entget ln) dx10 (dxf 10 l) dx11 (dxf 11 l)
          k (entget tx)  str (dxf 1  k))

    (if (setq z (distof str))   
      (LM:Update
        (LM:SubstDXF 10 (list (car dx10) (cadr dx10) z)
          (LM:SubstDXF 11 (list (car dx11) (cadr dx11) z) l)
        )
      )
      (princ "\n** Invalid Elevation **")
    )
  )

  (princ)
)

(defun dxf ( code lst ) (cdr (assoc code lst)))

(defun LM:SubstDXF ( code value elist )
  ;; Lee Mac 2010
  (entmod
    (subst
      (cons code value) (assoc code elist) elist
    )
  )
)

(defun LM:Update ( elist )
  ;; Lee Mac 2010
  (entupd
    (cdr (assoc -1 elist))
  )
)

;;-------------------=={ Select if Foo }==--------------------;;
;;                                                            ;;
;;  Continuous selection prompts until the predicate function ;;
;;  foo is validated                                          ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  foo - predicate function taking ename argument            ;;
;;  str - prompt string                                       ;;
;;------------------------------------------------------------;;
;;  Returns:  selected entity ename if successful, else nil   ;;
;;------------------------------------------------------------;;

(defun LM:SelectifFoo ( foo str / sel ent )
  (vl-load-com)
  ;; Lee Mac 2010
  (while
    (progn
      (setq sel (entsel str))
     
      (cond
        (
          (vl-consp sel)

          (if (not (foo (setq ent (car sel))))
            (princ "\n** Invalid Object Selected **")
          )
        )
      )
    )
  )
  ent
)
     

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adjust Z coordinates of lines, based on leader and text
« Reply #4 on: August 13, 2010, 11:52:44 AM »
Ok, I have found some problems with doing this:
  • Text is MTEXT with formatting
  • The lines that needed adjustment are actually plines
  • There are many lines at the end of some of the leaders that need to be adjusted (sometimes 20-30)
  • In some cases there are no lines at the end of the leader, when this happens a point needs to be created at the correct coordinates
The solutions that I have found are:
  • For the MTEXT I am reusing some code from http://www.theswamp.org/index.php?topic=31584.0 and adjusting it so that it can be called form within my routine.
  • This is fairly easy to get around by using VLA
  • I have opted for a hybrid method of the ones discussed above, select the text and leader and all lines that have an endpoint that matches the end of the leader will be selected.
  • If there are no lines that end at the end of the leader, then create a point.

Attached is the code that I have so far, most of this code works; however, for some reason pts2 always ends up being nil and I get an error message of ; error: lisp value has no coercion to VARIANT
with this type:  (6.38836e+006 1.88877e+006 6.38837e+006 1.88876e+006), although the coordinates will change.

I would appreciate any help in debugging it, I can't seem to figure out why it is doing what it is doing.