Jump to content

Recommended Posts

Posted

Bonsoir,

j'ai besoin d'un lisp pour lire un fichier DAT sur autocad.merçi

voici la structure du fichier txt (DAT) ci dessous : 

Plle n°1
18
B425 212744.77 223283.86
Br2 212784.27 223317.31
Br3 212815.88 223344.28
BR401 212838.92 223363.25
B400 212852.84 223373.15
B401 212846.86 223355.14
B402 212833.65 223324.66
B403 212828.60 223314.74
B404 212869.57 223297.64
B405 212884.75 223291.75
B407 212880.87 223283.55
B408 212945.74 223265.13
B409 212979.53 223256.34
B410 212991.78 223253.21
B421 212984.73 223224.08
B422 212869.16 223253.38
B423 212863.66 223238.85
B424 212803.73 223260.59

 

Posted

PLLE n ° 1--------------------------------------Num de Plle
18--------------------------------------------------Nombre de points
B425; 212.744,77 ;223.283,86--------Num de point ;X;Y

Posted
(defun c:MyImportDatTest ()
  (setq fp (getfiled "Select File:" "" "dat" 16))
  (if fp
    (progn
      (setq f (open fp "r"))
      (setq numDePlle (read-line f))
      (princ (strcat "\nNum De Plle: " numDePlle))
      (setq nombreDePoints (read (read-line f)))
      (princ (strcat "\nNombre De Points: " (rtos nombreDePoints 2 0 )))
      
      (while (setq line (read-line f))
	(setq tokens (dos_StrTokens line " "))
	(setq pName (nth 0 tokens))
	(setq pX (read (nth 1 tokens)))
	(setq pY (read (nth 2 tokens)))
	(princ (strcat "\n" pName "," (rtos px 2 2) "," (rtos py 2 2)))
	)
      )
    )
  (princ)
  )

 

Above is a quick example as to how to do this. Note: the above code does require Dos_Lib to be installed prior to running the code.

 

Dos_Lib can be downloaded here: https://wiki.mcneel.com/doslib/home

 

I hope that this helps,

 

regards,

 

hippe013

 

  • Like 1
Posted

It must be my eyes, but I cannot but wonder why you want to position batteries?

 

Perhaps something got lost in translation, and a request in english (being the language of this Forum) would help some.

 

2112893197_Plottingdata.PNG.60eba0983907269f6853b7097ee216e3.PNG

 

 

Posted
Good evening, dear friends, what I need is a lisp to import a file (* .txt) for several plots in the form of polygons on autocad. the structure of the file (* .txt) and as below:

P1
18
B425 212.744,77 223.283,86
Br2 212.784,27 223.317,31
Br3 212.815,88 223.344,28
BR401 212.838,92 223.363,25
B400 212.852,84 223.373,15
B401 212.846,86 223.355,14
B402 212.833,65 223.324,66
B403 212.828,60 223.314,74
B404 212.869,57 223.297,64
B405 212.884,75 223.291,75
B407 212.880,87 223.283,55
B408 212.94image.thumb.png.6e80be6503d8d40864fafb7e63ab952f.png5,74 223.265,13image.thumb.png.6e80be6503d8d40864fafb7e63ab952f.png
B409 212.979,53 223.256,34
B410 212.991,78 223.253,21
B421 212.984,73 223.224,08
B422 212869.16 223253.38
B423 212863.66 223238.85
B424 212803.73 223260.59

 

Pour nous donner le résultat suivants : 

cordialement

Fichier1.TXT

 

Posted (edited)

Hippe013 use Lee-macs parse lisp to look for space which is character 32 saves the need for DOSLIB its good but adds a complication to a novice user.

 

; thanks to Lee-mac for this defun 
; www.lee-mac.com
; 44 is comma
; 32 is a space
(defun _csv->lst ( str del / pos )
	(if (setq pos (vl-string-position del str))
		(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
		(list str)
    )
)
(setq str (read-line .........
(setq lst (_csv->lst str 32)) 

Re Ficheir1.txt Galtar53 extra code needed ok if always say a B or you can choose.

Edited by BIGAL
Posted (edited)

My $0.05 it could have layers etc but that is unknown same with Point as a block. 9546 points ? Entmake makes a huge difference compared to Command.

 

; https://www.cadtutor.net/forum/topic/70687-importer-fichier-dat-sur-autocad/
; Read multiple points skip non xy points
; By AlanH info@alanh.com.au

; thanks to Lee-mac for this defun 
; www.lee-mac.com
; 44 is comma
; 32 is a space
(defun _csv->lst ( str del / pos )
	(if (setq pos (vl-string-position del str))
		(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2)) del))
		(list str)
    )
)


(defun entpt ( ptxyz )
(entmake (list
'(0 . "POINT")
'(100 . "AcDbEntity")
'(100 . "AcDbPoint")
(cons 10 ptxyz)
)
)
)

(defun entxt ( txt )
(entmake (list
'(0 . "TEXT")
'(100 . "AcDbEntity")
'(100 . "AcDbText")
(cons 10 pt)
(cons 1 txt)
'(40 . 2.5)
'(50 . 0)
'(73 . 0)
)
)
)

(defun c:MyImportDatTest-2 ( / fp f ans pt txtans oldsnap)
(setvar 'pdmode 34)
(setvar 'pdsize 2)
(setq oldsnap (getvar 'osmode))
(setvar "osmode" 0)
  (setq fp (getfiled "Select File:" "" "txt" 16))
  (setq f (open fp "r"))
  (if fp
    (progn
	(while (setq newline (read-line f))
	  (setq ans (_csv->lst newline 32))
	  (if (= "B" (substr (car ans) 1 1))
	    (progn
	    (setq pt (list (atof (cadr ans)) (atof(caddr ans)) 0.0))
	    (setq txtans (car ans))
	    (entpt pt)
	    (entxt txtans)
	    )
	  )
    )
  )
)
(close f)
(setvar 'osmode oldsnap)
(command "zoom" "e")
(princ)
)
(c:MyImportDatTest-2)

 

Edited by BIGAL
Posted

Bigal,

 

Lee Mac is a darn good programmer. I am aware of his functions. However, the example that you posted appears to have an error in the recursive call. (Too few arguments) I also don't feel that installing dos_lib would be over-bearing to anybody wanting to learn lisp, beginner or otherwise. 

 

Galter53,

 

I have the following code that draws the polylines using the points in the file. 

I prefer using vla objects because it runs faster than using dxf codes and entmod. 

 

This version does use Bigal's recommendation of using Lee Mac's parsing function. (With the error corrected) 

 

(defun c:MyImportDatTest-3 ()
  (setq flag 0)
  (setq fp (getfiled "Select File:" "" "txt" 16))
  (setq pList nil)
  (if fp
    (progn
      (setq ms (vlax-get-property (vlax-get-property (vlax-get-acad-object) 'ActiveDocument) 'Modelspace))
      (setq f (open fp "r"))
      (while (setq line (read-line f))
	(setq tokens (_csv->lst line 32))
	(if (= flag 0)
	  (progn
	    (princ (strcat "\nCurrent Position: " line))
	    (setq flag 1)
	    )
	  (progn
	    (if (= flag 1)
	      (progn
		(princ (strcat "\nNumber of Points: " line))
		(setq flag 2)
		)
	      (progn
		(if (= flag 2)
		  (progn
		    
		    (if (= 3 (length tokens))
		      (progn
			(princ (strcat "\n    " (nth 0 tokens)))
			(setq x (read (nth 1 tokens)))
			(setq y (read (nth 2 tokens)))
			(setq pList (append pLIst (list x y)))
			)
		      (progn
			(setq flag 0)
			(princ (strcat "\nCurrent Position: " line))
			(setq flag 1)
			(setq pl-Obj (vlax-invoke-method ms 'AddLightweightPolyline (pl->var pList)))
			(vlax-put-property pl-Obj 'Closed :vlax-True)
			(setq pList nil)
			)
		      );End If token length = 3
		    )
		  );End If flag = 2
		)
	      );End If flag = 1
	    )
	  );End If flag = 0
	);End While
      )
    );End If fp
  (princ)
  );End Defun




(defun _csv->lst ( str del / pos )
	(if (setq pos (vl-string-position del str))
		(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2)) del))
		(list str)
    )
)


 (defun pl->var (pl / ub sa var)
    (setq ub (- (length pl) 1))
    (setq sa (vlax-make-safearray vlax-vbdouble (cons 0 ub)))
    (setq var (vlax-make-variant (setq sa (vlax-safearray-fill sa pl))))
    )

 

 

Let me know if this works for you.

 

regards,

 

hippe013

Posted (edited)

Hippe013 put the extra del into the code and changed the option for space or comma as per lees original code the second del got lost somewhere. Interesting is that it was working, Huh ?

 

New code includes plines using entmake. Was not sure about closed , Trudy yes or no change (70 . 1) for closed or repeat 1st point as last point with (70 . 0) for those you want to appear closed.

 

; https://www.cadtutor.net/forum/topic/70687-importer-fichier-dat-sur-autocad/
; Read multiple points skip non xy points
; By AlanH info@alanh.com.au

; thanks to Lee-mac for this defun 
; www.lee-mac.com
; 44 is comma
; 32 is a space
(defun _csv->lst ( str del / pos )
	(if (setq pos (vl-string-position del str))
		(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2)) del))
		(list str)
    )
)

(defun LWPoly (lst cls)
 (entmakex (append (list (cons 0 "LWPOLYLINE")
                         (cons 100 "AcDbEntity")
                         (cons 100 "AcDbPolyline")
                         (cons 90 (length lst))
                         (cons 70 cls))
                   (mapcar (function (lambda (p) (cons 10 p))) lst)))
)

(defun entpt ( ptxyz )
(entmake (list
'(0 . "POINT")
'(100 . "AcDbEntity")
'(100 . "AcDbPoint")
(cons 10 ptxyz)
)
)
)

(defun entxt ( txt )
(entmake (list
'(0 . "TEXT")
'(100 . "AcDbEntity")
'(100 . "AcDbText")
(cons 10 pt)
(cons 1 txt)
'(40 . 2.5)
'(50 . 0)
'(73 . 0)
)
)
)

(defun c:MyImportDatTest-2 ( / fp f ans pt txtans oldsnap)
(setvar 'pdmode 34)
(setvar 'pdsize 2)
(setq oldsnap (getvar 'osmode))
(setvar "osmode" 0)
  (setq fp (getfiled "Select File:" "" "txt" 16))
  (setq f (open fp "r"))
  (if fp
    (progn
	(setq lst2 '())
	(while (setq newline (read-line f))
	  (setq ans (_csv->lst newline 32))
	  (if (= "B" (substr (car ans) 1 1))
	    (progn
	    (setq pt (list (atof (cadr ans)) (atof(caddr ans)) 0.0))
        (setq lst2 (cons pt lst2))
	    (setq txtans (car ans))
	    (entpt pt)
	    (entxt txtans)
		(setq pl "Y")
	    )
		(progn
		(if (= pl "Y")
		(LWPoly lst2 0)
		)
		(progn
		(setq pl "N")
		(setq lst2 '())
		)
		)
	  )
	)
	(LWPoly lst2 0)
    )
  )
(close f)
(setvar 'osmode oldsnap)
(command "zoom" "e")
(princ)
)
(c:MyImportDatTest-2)

image.png.47e8884738b7204ba47e8b6058e0a510.png

 

Edited by BIGAL

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