Jump to content

Rotation of block Through Excel


pmadhwal7

Recommended Posts

I have multiple block and i have xy and rotation where the block to be placed and i want to import those from excel to autocad with rotation

Link to comment
Share on other sites

Here is an example.  It works for CSV files. 

Make sure it's semicolon delimited, and has numbers with a point, not with commas (Depends on Excell settings)

 

Make sure the layers and blocks that appear in your CSV file exist in your dwg. 

(In my example I have Layer1, Layer2, Layer3; and I have Block "A" and "B", see the attachments)

 

Best save the csv in the same folder as the DWG, that's where the file dialog will start looking first.

 



;; http://www.lee-mac.com/readcsv.html
;; 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 (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))
            (while (setq str (read-line des))
                (setq lst (cons (LM:csv->lst str sep 0) lst))
            )
            (close des)
        )
    )
    (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
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; https://www.cadtutor.net/forum/topic/18257-entmake-functions/
(defun drawInsert (pt Nme rot lay)
 (entmakex (list (cons 0 "INSERT")
                 (cons 2 Nme)
                 (cons 50 rot)
                 (cons 8 lay)
                 (cons 10 pt)))
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Degrees to rad
(defun d2r (d /)
  ( * pi (/ d 180.0) )
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(vl-load-com)

;; Read From Csv
(defun rfc ( / csvfile data i row)
  ;; settings
 
  (setq csvfile (getfiled "Select File" (getvar 'dwgprefix) "csv" 16))
 
  (setq data (LM:readcsv csvfile))
  (setq i 0)
  (foreach row data
    (if (> i 0) (progn    ;; skip the title row
      ;; (list "Layer" "X" "Y" "Blockname" "Rotation")
      (drawInsert
            (list (atof (nth 1 row)) (atof (nth 2 row)))         ;; IP
            (nth 3 row)                                          ;; Blockname
            (d2r (atof (nth 4 row)))                             ;; rotation, imput is in degrees, gets converted by d2r
            (nth 0 row)                                          ;; Layer
      )
    ))
    (setq i (+ i 1))
  )  
  (princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; COMMANDS

;; Import Blocks From Csv
(defun c:ibfc ( / )
  (rfc)
  (princ)
)

(princ "\nCommand: IBFC to import blocks: ")
(princ)

 

import_blocks.dwg import_block.csv

Edited by Emmanuel Delay
Link to comment
Share on other sites

Try this routine.

 

(defun c:test (/ csv opn rtn str bkn)
  (and
    (setq csv (getfiled "Select csv file" (getvar 'DWGPREFIX) "csv" 16))
    (setq opn (open csv "r"))
    (while (setq rtn (read-line opn))
      (setq str (_peelstring rtn ";"))
      (and
        (tblsearch "BLOCK" (setq bkn (nth 3 str)))
        (entmake
          (list '(0 . "INSERT")
                (cons 2 bkn)
                (cons 8 (car str))
                (cons 10
                      (list (distof (cadr str)) (distof (caddr str)) 0.0)
                )
                (cons 50 (distof (nth 4 str)))
          )
        )
      )
    )
    (close opn)
  )
  (princ)
)
(defun _peelstring (string del / str pos lst)
  ;; Tharwat - date: 07.Oct.2015	;;
  (while (setq pos (vl-string-search del string 0))
    (setq str    (substr string 1 pos)
          string (substr string (+ pos (1+ (strlen del))))
    )
    (and str (/= str "") (setq lst (cons str lst)))
  )
  (and string (/= string "") (setq lst (cons string lst)))
  (reverse lst)
)

 

Link to comment
Share on other sites

thanks both of you when i insert block in my file the angle of block is not as i desired u can check my attached dwg what wrong thing i am doing, please check , my reference of angle is sheet number text in my dwg, as i exporting the sheet number text and copy the angle of those text then putting it in block rotation column

input.dwg

Link to comment
Share on other sites

On 11/12/2019 at 12:44 PM, Tharwat said:

Try this routine.

 


(defun c:test (/ csv opn rtn str bkn)
  (and
    (setq csv (getfiled "Select csv file" (getvar 'DWGPREFIX) "csv" 16))
    (setq opn (open csv "r"))
    (while (setq rtn (read-line opn))
      (setq str (_peelstring rtn ";"))
      (and
        (tblsearch "BLOCK" (setq bkn (nth 3 str)))
        (entmake
          (list '(0 . "INSERT")
                (cons 2 bkn)
                (cons 8 (car str))
                (cons 10
                      (list (distof (cadr str)) (distof (caddr str)) 0.0)
                )
                (cons 50 (distof (nth 4 str)))
          )
        )
      )
    )
    (close opn)
  )
  (princ)
)
(defun _peelstring (string del / str pos lst)
  ;; Tharwat - date: 07.Oct.2015	;;
  (while (setq pos (vl-string-search del string 0))
    (setq str    (substr string 1 pos)
          string (substr string (+ pos (1+ (strlen del))))
    )
    (and str (/= str "") (setq lst (cons str lst)))
  )
  (and string (/= string "") (setq lst (cons string lst)))
  (reverse lst)
)

sir the lisp is working but the problem is rotation angle can you please check my dwg

 

input.dwg

Link to comment
Share on other sites

43 minutes ago, Tharwat said:

Did you read my last reply ?

sir my reference is sheet number text from their i am copying angle for placement of block

Link to comment
Share on other sites

2 hours ago, pmadhwal7 said:

sir my reference is sheet number text from their i am copying angle for placement of block

Just correct the angle value in the Excel file since the program gets the radians from there.  

Link to comment
Share on other sites

14 hours ago, Tharwat said:

Just correct the angle value in the Excel file since the program gets the radians from there.  

is it possible that math block with text like match properties command?

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