Jump to content

transform polyline coordinates


Trudy

Recommended Posts

Hello, i want to ask if it possible to create lisp who can change coordinates on multiple polyline.

Something like, few polylines with coordinates (№   X     Y)

                                                                                    1  100   200

                                                                                    2  200   250

                                                                                    3  300   210

                                                                                    4  150   220

                                                                                    5  110   250

and i want to export in .txt file all of the lines and their coordinates and change them, after the change i want to replace this coordinate(№ 1,2,3,4,5) with the new.

Something like export-->change-->and replace

Same line but with other coordinate.

 

Sorry for my english.

Thak you :)

 

 

Link to comment
Share on other sites

yes you can either entmod or entmake with new coordinates data.

what do you mean transform here? UCS to WCS or vice versa. trans may help

Lot's of examples to export polyline *.txt file , etc..

Don't reinvent the wheel :) 

Try search with related key words.. 

 

 

Link to comment
Share on other sites

I use other program for transform coordinate and this program work with .txt file (№ X Y)

This is the reason to export .txt file with coordinate and replace the new coordinate.

Link to comment
Share on other sites

On 11/11/2019 at 20:55, Trudy said:

Hello, i want to ask if it possible to create lisp who can change coordinates on multiple polyline.

Something like, few polylines with coordinates (№   X     Y)

                                                                                    1  100   200

                                                                                    2  200   250

                                                                                    3  300   210

                                                                                    4  150   220

                                                                                    5  110   250

and i want to export in .txt file all of the lines and their coordinates and change them, after the change i want to replace this coordinate(№ 1,2,3,4,5) with the new.

Something like export-->change-->and replace

Same line but with other coordinate.

 

Sorry for my english.

Thak you :)

 

 

 

Using Visual Lisp you can "get" and "put" the "coordinates" property of polyline objects, and reading and writing to text files is also simple. You would however have to separate the export from the import. Are the numbers (No)  vertices?

 

 

Link to comment
Share on other sites

Like dlanorh you need the "handle" of the pline this is the object ID if you close the dwg etc and reopen the "handle" is still correct. Need error check for exists.

 

A0
1  100   200 
2  200   250 
3  300   210 
4  150   220 
5  110   250


AO
1  1100   2200 
2  1200   2250 
3  1300   2210 
4  1150   220 
5  1110   2250


(if (not (setq e1 (handent "A0")))
  (princ "\nNo entity with that handle exists. ")
  (princ e1)
)
<Entity name: 3ac6c030>

 

Link to comment
Share on other sites

i want to change the coordinate from selected multiple polyline, but for now 

(defun c:ttt ()
(setq sel (ssget '((0 . "lwpolyline"))))
(setq objj (ssname sel 0))
(setq obj (vlax-ename->vla-object objj))
(setq aa1 (vlax-get obj 'Coordinates))
(princ aa1)
(vlax-put obj 'Coordinates '(35.3186 1547.91 615.548 ))
)

 

I do this for now but select only one line 

Link to comment
Share on other sites

Ok will not work (vlax-put obj 'Coordinates '(35.3186 1547.91 615.548 ))

 

(vlax-put obj 'Coordinates a pline is (X Y X Y X Y...…) so a pline must have at least 4 values in the list (1 1 2 2)

 

As I asked already you need to explain more its not a hard task 

 

something like

1,35.3186,1547.9

5,72.62,63.86

12,103.25,78.98

This would be a pline with minimum 12 vertices change 1 or more.

Link to comment
Share on other sites

yes i just delete some of the coord becaus the polyline have 10 vertext but this work for 1 polyline. 

And i want to select maybe 30 polyline and change all of their coordinates, but in this way "ssget" select the polylines and give properties of only one coord.

And other problem is if i select line with 2 vertext but have more than 4 coordinates the line get 1 more vertext but i dont want it.

I want to change the coordinates for all lines but for each other.

Link to comment
Share on other sites

(defun c:Plexport(/ *error* file sset  ename lst)

     (vl-load-com)
     (defun *error* (s)
          (if file (close file))
          (cond
       ( ( not s ) )
       ( (member s '("Function cancelled" "quit / exit abort") ) )
       ( (princ (strcat "\n---->Error:" s) ) )
   )
       (princ))
       (if (setq sset (ssget "_:L" '((0 . "LWPOLYLINE"))))
    (progn
         (if (setq file (open (strcat (getvar 'dwgprefix) "transform.txt") "w"))
      (progn
           (write-line (strcat "X" "," "Y") file)
           (repeat (setq i (sslength sset))
         (setq ename (vlax-ename->vla-object (ssname sset (setq i (1- i)))))
         (setq lst (vlax-safearray->list
       (vlax-variant-value
         (vla-get-coordinates ename)
       )
     )
         )
		 (princ lst)
         (repeat (/ (length lst) 2)
       (write-line (strcat (rtos (car lst)) "," (rtos (cadr lst))) file)
       (setq lst (cddr lst))
         )
    )
           (close file)
           (alert "\nVertex Points exported to csv file.")
           (alert (strcat "File saved in - "(getvar 'dwgprefix) "transform.txt"))
      )
      (alert "\nCSV file Currenty running, Close it first.")
  )
     )
     (*error* "Nothing Selected.")
 )
        (*error* nil)
        (princ)
 )

I change the code little, this is what i look but this is the half of the code second part is to put the new coordinates.

I attach .dwg file

Sorry for late reply BIGAL. Thank you 

test1.dwg

Link to comment
Share on other sites

Try this need some real before - after to make sure I don't have the list of points backwards. There are a couple of areas that could be simplified. Thanks also to lee-mac.

 

(vl-load-com)
(defun *error* (s)
  (if file (close file))
  (cond
    ((not s))
    ((member s '( "Function cancelled" "quit / exit abort")))
    ((princ (strcat "\n---->Error:" s)))
  )
  (princ)
  )

(defun c:Plexport (/ *error* file sset ename lst)
  (if (setq sset (ssget "_:L" '((0 . "LWPOLYLINE"))))
    (progn
      (if (setq file (open (strcat (getvar 'dwgprefix) "transform.txt") "w"))
        (progn
          (write-line (strcat "X" "," "Y") file)
          (repeat (setq i (sslength sset))
            (setq ename (vlax-ename->vla-object (ssname sset (setq i (1- i)))))
            (setq lst (vlax-safearray->list (vlax-variant-value (vla-get-coordinates ename))))
            (princ lst)
            (repeat (/ (length lst) 2)
              (write-line (strcat (rtos (car lst)) "," (rtos (cadr lst))) file)
              (setq lst (cddr lst))
            )
          )
          (close file)
          (alert "\nVertex Points exported to csv file.")
          (alert (strcat "File saved in - " (getvar 'dwgprefix) "transform.txt"))
        )
        (alert "\nCSV file Currenty running, Close it first.")
      )
    )
    (*error* "Nothing Selected.")
  )
)




;; Read CSV  -  Lee Mac
;; Parses a CSV file into a matrix list of cell values.
;; csv - [str] filename of CSV file to read

(defun LM:readcsv (csv / des lst sep str)
  (if (setq des (open csv "r"))
    (progn
      (setq sep ",")
      (while (setq str (read-line des))
        (setq lst (cons (LM:csv->lst str sep 0) lst))
      )
      (close des)
    )
    (progn
      (alert "File not found \n \n Now exiting")
      (exit)
    )
  )
  (reverse lst)
)

;; CSV -> List  -  Lee Mac
;; Parses a line from a CSV file into a list of cell values.
;; str - [str] string read from CSV file
;; sep - [str] CSV separator token
;; pos - [int] initial position index (always zero)

(defun LM:csv->lst (str sep pos / s)
  (cond
    ((not (setq pos (vl-string-search sep str pos)))
      (if (wcmatch str "\"*\"")
        (list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2))))
        (list str)
      )
    )
    ((or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]")
         (and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos)))
     )
       (LM:csv->lst str sep (+ pos 2))
    )
    ((wcmatch s "\"*\"")
              (cons
                (LM:csv-replacequotes (substr str 2 (- pos 2)))
                (LM:csv->lst (substr str (+ pos 2)) sep 0)
              )
    )
    ((cons s (LM:csv->lst (substr str (+ pos 2)) sep 0)))
  )
)

(defun LM:csv-replacequotes (str / pos)
  (setq pos 0)
  (while (setq pos (vl-string-search "\"\"" str pos))
    (setq str (vl-string-subst "\"" "\"\"" str pos)
          pos (1+ pos)
    )
  )
  str
)

;;;starts here
(defun c:Plimport (/ *error* file sset ename x lst)
 ; start at 1 so skips header line.
  (setq csvlst (LM:readcsv (strcat (getvar 'dwgprefix) "transform.txt")))
  (setq x 1)
  (setq lst2 '())
  (repeat (- (length csvlst) 1)
    (setq lst2 (cons (atof (nth 1 (nth x csvlst))) lst2))
    (setq lst2 (cons (atof (nth 0 (nth x csvlst))) lst2))
    (setq x (+ x 1))
  )

  (setq obj (vlax-ename->vla-object (car (entsel "pick pline object"))))
  (if (and (= (vla-get-ObjectName obj) "AcDbPolyline")
           (= (length lst2) (length (vlax-get Obj 'coordinates)))
      )
    (vlax-put Obj 'coordinates lst2)
    (progn
      (alert "You have picked a pline with different total vertices \n \n Now exiting")
      (exit)
    )
  )

  (princ)
)
These do the same
(setq lst (vlax-safearray->list (vlax-variant-value (vla-get-coordinates ename))))
(setq lst (vlax-get ename 'coordinates))

 

Edited by BIGAL
Link to comment
Share on other sites

Another problem 

(defun c:Plimport (/ *error* file sset ename x lst)
 ; start at 1 so skips header line.
  (setq csvlst (LM:readcsv (strcat (getvar 'dwgprefix) "transformt.txt")))
    (setq csvlst1 (LM:readcsv (strcat (getvar 'dwgprefix) "transform.txt")))
(setq sset (ssget '((0 . "LWPOLYLINE"))))
(progn
(repeat (setq i (sslength sset))
(setq aa (ssname sset (setq i (1- i))))
(setq ed (entget aa))
(setq ed1 (subst '(10 2279.48 -1055.16) '(10 6279.48 -1055.16) ed))
;(princ ed1)
(entmod ed1)
  ) ;end repeat  
   ) ;end progn
  (princ)
)

when i use this code its work but its only for one vertext

 

(defun LM:group<n ( l n / a b m )
    (while l
        (setq m n)
        (while (and l (< 0 m))
            (setq a (cons (car l) a)
                  l (cdr l)
                  m (1- m)
            )
        )
        (setq b (cons (reverse a) b)
              a nil
        )
    )
    (reverse b)
)

(defun c:Plimport (/ *error* file sset ename x lst)
 ; start at 1 so skips header line.
  (setq csvlst (LM:readcsv (strcat (getvar 'dwgprefix) "transform.txt")))

  (setq csvlst1 (LM:readcsv (strcat (getvar 'dwgprefix) "transformt.txt")))
  (setq x 0)
  (setq lst2 '())
  (repeat (length csvlst1)
    (setq lst2 (cons (atof (nth 1 (nth x csvlst1))) lst2))
    (setq lst2 (cons (atof (nth 0 (nth x csvlst1))) lst2))
    (setq x (+ x 1))
  )
  (setq jj nil)
  (setq ll nil)
(setq aw (LM:group<n lst2 2))
(setq jj (cons 10 (car (reverse aw))))



  (setq y 0)
  (setq lst '())
  (repeat (length csvlst)
    (setq lst (cons (atof (nth 1 (nth y csvlst))) lst))
    (setq lst (cons (atof (nth 0 (nth y csvlst))) lst))
    (setq y (+ y 1))
  )
(setq al (LM:group<n lst 2))
(setq ll (cons 10 (car al)))
(princ ll)


		
(setq sset (ssget '((0 . "LWPOLYLINE"))))
(progn
(repeat (setq i (sslength sset))
(setq aa (ssname sset (setq i (1- i))))
(setq ed (entget aa))
(setq ed1 (subst jj ll ed))
(princ ed1)
(entmod ed1)
  ) ;end repeat  
   ) ;end progn
  (princ)
)

I change this code and i try with entmod i work but only for first vertext i try with other vertext but didnt work. I dont know how to "cons" all couple of coordinates and replace one by one from the list.

(defun c:try1 ()
(setq sset (ssget '((0 . "LWPOLYLINE"))))
(progn
(repeat (setq i (sslength sset))
(setq aa (ssname sset (setq i (1- i))))
(setq ed (entget aa))
(setq ed (subst (cons 8 "5") (assoc 8 ed) ed))
(entmod ed)
  )
  )
  (princ)
  )

with the layer all work fine. but with the coordinates i try maybe 10 hours and nothing.

 

Thank you all for the help.

transform.txt transformt.txt

Link to comment
Share on other sites

If you use the code I posted it will work, you have dropped the x,y at start so a couple of changes are needed, you seem to be changing the rules as we go on.

 

In vlisp there are two otpions 'coordinates and 'coordinate the second means just that change only 1 vertice. 

 

(setq obj (vlax-ename->vla-object (car (entsel "pick object"))))
(setq x (getreal "enter x value"))
(setq y (getreal "enter y value"))
(setq num (getint "enter vertice value"))
(vlax-put Obj 'coordinate num (list X Y) ; will change the 3rd vertice value only i

And yes you can pick a vertice and get its number is this what you wanted from the start ?

Edited by BIGAL
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...