Jump to content
pmadhwal7

Rotation of block Through Excel

Recommended Posts

pmadhwal7

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

Share this post


Link to post
Share on other sites
Emmanuel Delay

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

Share this post


Link to post
Share on other sites
pmadhwal7

sir i tried your lsp in my attached file but found this type of error (; error: bad argument type: stringp nil)

input.dwg output.dwg

Book2.csv

Edited by pmadhwal7

Share this post


Link to post
Share on other sites
pmadhwal7

any solution sir?

Share this post


Link to post
Share on other sites
Tharwat

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

 

Share this post


Link to post
Share on other sites
Emmanuel Delay

That's weird.  My code works for your drawings and csv on my pc.

Share this post


Link to post
Share on other sites
pmadhwal7

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

Share this post


Link to post
Share on other sites
Tharwat

The rotation value in the Excel file is incorrect and that's why the rotation of the inserted block is also incorrect.

Share this post


Link to post
Share on other sites
pmadhwal7
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

Share this post


Link to post
Share on other sites
Tharwat

Did you read my last reply ?

Share this post


Link to post
Share on other sites
pmadhwal7
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

Share this post


Link to post
Share on other sites
Tharwat
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.  

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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