Block Import Export v2.0

Block Import Export v2.0

DonatasAzaravicius
Advocate Advocate
2,269 Views
14 Replies
Message 1 of 15

Block Import Export v2.0

DonatasAzaravicius
Advocate
Advocate

Hi,

I have made new block import - export command in lisp. It uses OpenDCL to work. So it will need to be installed if you want to use this lisp. After you install OpenDCL and put .lsp and .odcl files in AutoCAD search path you can enter command BlockImportExport

I have made video about how it works

 

After you enter command a window will open

BlockImportExport.png

In section "Blocks" you can see all existing blocks in current drawing. Only three is shown at once but you can slide them with mouse. You can also double clock a block to preview it in bigger window. Button below is for importing new blocks from different drawing file.

Here are few delimiters to choose for importing from csv file, but I used only Auto and it always worked. You can also select Custom and enter your own delimiter.

To left here are 6 buttons, but I have only used 3 of al of them:

  • Import button will import csv or txt file with blocks data. I don't use this one.
  • Past button will open new window where you can copy-past blocks data from excel directly. I only use this for importing blocks.
  • Export button will let choose to options for exporting blocks from drawing. All blocks of same name or blocks from selection. I often use this button.
  • Copy button will open new window with blocks data selected for export. You can copy it and past directly to excel. I use this for export.
  • Ok button will import blocks to drawing file if Import or Past buttons have been pressed. If Export button was pressed it will late save new csv file with blocks data. I don't use this button for exporting, because I only use Copy button.
  • Cancel button will close Import and Export window.

In information section you can see block names for export or import and they count. If you are importing block and block definition don't exist in current drawing file comment column will have comment about it. You can see it in video. You can import and export multiple blocks at same time, but if you exporting blocks you should remember, if you select same block in multiple selections of Export button you will have multiple copies of same block.

At the bottom is spreadsheet with blocks data.

 

If you have more questions, you can ask them here.

 

P.S. CSV file needs to have this columns: Block name, X coordinate, Y coordinate, Elevation

These four are essential. Without them import will not work. After them here can be any number of attribute data, or none.

P.S.S Columns in window are named: Name, Y, X, Elevation. This is because in my country AutoCad coordinate X is named Y and coordinate Y is named X.

You can change names in lsp file at lines 42 and 43

  ; Columns names for blocks data grid.
  (dcl-Grid-AddColumns BlockIE/Main/GridData 
    (list
      (list "Name" 0 100)
      (list "Y" 1 80)
      (list "X" 1 80)
      (list "Elevation" 1 70)
    )
  )
Accepted solutions (1)
2,270 Views
14 Replies
Replies (14)
Message 2 of 15

bimFVC9B
Explorer
Explorer

Thank you very much for sharing your code.

Message 3 of 15

Sea-Haven
Mentor
Mentor

Have you thought about read from Excel rather than use CSV ? Its not that hard.

0 Likes
Message 4 of 15

DonatasAzaravicius
Advocate
Advocate

I only thought about things which are useful to me or may be useful to me. Right now I only use Copy-Past functionality and don't use csv. So, naturally I will not be interested in reading from Excel.

But others are free to modify my code and add functionality they need.

Message 5 of 15

quangdiepctmbk
Explorer
Explorer

When click ok button, show error "activex server returned an error" - invalid index on autocad 2023. Please help me. Thank you.

0 Likes
Message 6 of 15

DonatasAzaravicius
Advocate
Advocate

Hi,

I need more information.

1) Do you get error when you are importing blocks or when you are exporting blocks or on both?

2) Did you tried to use command on different types of blocks? For example: You try to import/export block A and get an error. Run command again and try import/export block B. It is best to try different blocks, may be problem is with block definition?

3) When you try to import blocks, does data in csv file have all information for all block attributes, or are some missing?

 

Can you attach empty dwg file with only block definition you want to import (for example one block in dwg file) and csv file with block data to import? I would try on my AutoCAD.

Could you make print Screen of Block Import - Export window before you press Ok button and attach it here too?

Message 7 of 15

quangdiepctmbk
Explorer
Explorer

Hi

I send the operation screen window. Please check att file. 

Thank you.

0 Likes
Message 8 of 15

DonatasAzaravicius
Advocate
Advocate

I found where problem is.

Your blocks don't have attributes, but you try to add attributes to them.

If you where to try to import only:

Name X Y Elevation

 

block1	10	10	0
block2	15	15	0

 

 It will import both blocks.

But if you try to import attributes too (if your blocks definitions don't have attributes).

Name X Y Elevation Attr1

 

block1	10	10	0	1
block2	15	15	0	1

 

It will return error and will not import blocks.

 

So, you should remove attribute data from csv if your block don't have attributes.

 

Update 1

I found where problem come from. It comes from function DA:InsertBlock at 862 line.

This function first insert new block in specified coordinates and gets reference to inserted block.

After it checks if function got attribute data (from csv file or other ways), but this function don't check if inserted block have attributes.

If function got attributes it try to get attributes references from block and modify them with this line

(vlax-safearray->list (vlax-variant-value (vla-getattributes BlRefObj)))

 and vlax-safearray->list returns error, because block don't have attributes.

I think of changing 867 line to check not only presence of attribute data, but also to check if block have attributes

(if (and Attributes (= 2 (cdr (assoc 70 (tblsearch "block" BlockName)))))

 I don't know if this will work for all blocks, because dxf 70 is bit codded value, but it should work for normal blocks.

Message 9 of 15

DonatasAzaravicius
Advocate
Advocate
Accepted solution

Block Import - Export v2.1

 

Fixed bug for trying to add attributes to blocks who don't have attributes.

Message 10 of 15

quangdiepctmbk
Explorer
Explorer

Thank you very much.

Message 11 of 15

quangdiepctmbk
Explorer
Explorer

Hi

Currently this autolisp insert block  by X Y Z.Could you add rotation function?

Thank you.

0 Likes
Message 12 of 15

DonatasAzaravicius
Advocate
Advocate

This lisp is intended only for block import with attributes. Adding more functionality would increase complexity of data to import.

To import block with rotation essential data would need to be increased. For example

Name, X, Y, Z, Rx, Ry, Rz, Attr0, ...,  Attr[n]

This who don't need to rotated would need to insert 0 rotation for all 3 directions.

 

And most importantly I made it for my personal use, I don't really have motivation to add something I will not use. I already have many ideas for lisp and C++ commands to automate my work, but I don't have motivation to make them, because they are complicated and I don't use them often.

 

But anyone can modify my code and add functionality they need. Learning how to do it yourself would let you create other things no one else created or modify already existing lisp to your needs. Lisp is not hard 🙂

Message 13 of 15

Sea-Haven
Mentor
Mentor

Did this somewhere random blocks with and without attributes I think I used Vl-cmdf so will work with or without attributes.

"-insert block1 s 1 pt 0 att1 att2"

"-insert block2 s 1 pt 0"

recognised the csv pattern block1,x,y,z,1,0,,, no attributes, pretty sure convert csv to list. If want 1st attributes to be blank need say a space.

0 Likes
Message 14 of 15

DonatasAzaravicius
Advocate
Advocate

It is possible to do in different ways. But because this lisp is Import - Export, it would expect to export blocks with rotation data too. So, I would need to find how check block rotation at export time too.

It would be possible to add check box named "With rotation". If it is checked command would expect block data with rotation data and it would export blocks with rotation data. If it is not checked, command would expect here are no rotation data and would export without rotation data.

0 Likes
Message 15 of 15

quangdiepctmbk
Explorer
Explorer
Hi
I understand.Thank you for support.