1

Is there any way to change dwg file AutoCAD to png format or pdf format using python.

I tried looking many documentation none of them answered my question

```
from __future__ import print_function
from os.path import join, dirname, abspath
from xlutils.copy import copy
import xlrd
import xlwt
from pyautocad import Autocad, APoint
import os
import win32com.client
from pyautocad import Autocad, APoint
from pyautocad.contrib.tables import Table
from comtypes import COMError
def props(cls):
  return [i for i in cls.__dict__.keys() if i[:1] != '_']
# Create workbook
book = xlwt.Workbook()
ws = book.add_sheet("ExportedData")
book.save("Exported.xls")

# Open the workbook
xl_workbook = xlrd.open_workbook("Exported.xls")
sheet_names = xl_workbook.sheet_names()

xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])

wb = copy(xl_workbook)
sheet = wb.get_sheet(0)

dwgfiles = filter(os.path.isfile, os.listdir(os.curdir))



cwd = os.path.abspath(os.path.curdir)  # current working dir
print(cwd)


for f in dwgfiles:
    print("++++++++++++++++++++++++++++++")
    print("++++++++++++++++++++++++++++++")
    print("++++++++++++++++++++++++++++++")
    print("++++++++++++++++++++++++++++++")

    print(f)
    if f.endswith(".dwg"):
        print("sdaasdas")
        """ open Document"""
        acad = Autocad()
        print(cwd)
        acad.app.Documents.open(cwd + "/" + f)
        exportFile="new2.bmp"





        num_cols = xl_sheet.ncols  # Number of columns
        idx = 1

        acad = win32com.client.Dispatch("AutoCAD.Application")

        doc = acad.ActiveDocument  # Document object
        print(dir(doc))
        doc.Export('exportFile','bmp')
        print("MODEL SPACE")
        count=0
```

please help me to solve this?. the error I am getting is given below

**e_to_string_', '_username_', '_wrap_dispatch_']
Traceback (most recent call last):
  File "auto1.py", line 63, in <module>
    doc.Export(exportFile,"bmp")
  File "<COMObject <unknown>>", line 3, in Export
pywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)**

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

glibdud
  • 7,550
  • 4
  • 27
  • 37
Nithish Albin
  • 352
  • 5
  • 20

2 Answers2

1

I think the best way is to run command _-PLOT, and use virtual PDF ploter. here You can find more info. To run command You can use:

  acad.doc.SendCommand("_-PLOT" paramerets )

where paramerets define paper size, Plot area and so one.

I don't have any sample in python, just LISP. I hope You will handle translation. So the simplest sample may be:

(setq parameters (list "_n" layoutName "" "PDFCreator" "_n" "_n" "_y")

Where layoutName is variable in my case layoutName is read be iterate each layout. In this case all options will be default - You need to init configuration in plot window and use the button [Apply to Layout].

The more complicated example may be:

(setq parameters (list 
"_y"            ; detailed configuration
"Layout1"       ; Layout name
"PDFCreator"    ; ploter name 
"A4"            ; page size
""              ; drawing units
""              ; drawing orientation
""              ; plot upside-down
"_w"            ; plot area - window
(strcat (rtos(car P1)) "," (rtos(cadr P1)))     ; P1 and P2 are points ( three elemenets list of coordinates as real value ))
(strcat (rtos(car P2)) "," (rtos(cadr P2)))     ; we need to convert real values to strings
""              ; drawing scale
""              ; plot offset
""              ; use plot style table
""              ; plot styme name
""              ; Lineweight
""              ; Lineweight scale
""              ; plot paperspace first
""              ; plot paperspace objects
""              ; save to file ( *.plt)
"_n"            ; save changes to page configuration
"_y"            ; confirm
)
CAD Developer
  • 1,532
  • 2
  • 21
  • 27
  • doc.SendCommand('_-PLOT ' 'yes ' 'base1 ' 'PublishToWeb PNG.pc3 ') command is not working – Nithish Albin Apr 02 '19 at 05:03
  • 1
    "not working" may be understood in a hundreds different ways. I'm not going to guess which case you faced now. – CAD Developer Apr 02 '19 at 05:59
  • @ CAD Developer Now I am getting the file save dialogue box for saving png. Is there is any way to override it also – Nithish Albin Apr 02 '19 at 13:31
  • 1
    I suppose You can do it in the configuration of PublishToWeb PNG.pc3, but as I mentioned I'm working on ZWCAD, so I don't know details of Acad tool PublishToWeb PNG.pc3. – CAD Developer Apr 02 '19 at 13:53
  • @ CAD Developer Can we use the same code for ZWCAD . Is it also have activex api – Nithish Albin Apr 03 '19 at 04:31
  • 1
    I have a problem with understood limits of "ActiveX" API. ZWCAD support VBA so also ActiveX API. Some Delphi applications use ZWCAD, so I suppose the answer should be "Yes". But I never saw any project in python using ZWCAD so I can neither confirm nor deny. – CAD Developer Apr 03 '19 at 04:54
  • commad_str = "-PLOT yes base1\r\nPublishToWeb PNG.pc3\rSun Hi-Res (1600.00 x 1280.00 Pixels)\rP\rN\rE\rF\rC\rY\r.\rY\rN\rN\rN\nD:/autocad_test/see.png\n\nN\r\nY" doc = acad.ActiveDocument doc.SendCommand(commad_str) Can you check this code . what went wrong . I am getting save dialogue box .overide save dialogue. save to path mensioned in command – Nithish Albin Apr 03 '19 at 04:56
  • 1
    ZWCAD have no implementation of virtual plotter PublishToWebPNG.pc3 instead I have ZWCAD Virtual PNG Plotter.pc5. When I run the command to this device I also have the save dialogue. didn't found the configuration to disable it. As I remember I have done it years ago using PDF Creator. – CAD Developer Apr 03 '19 at 05:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191141/discussion-between-nithish-albin-and-cad-developer). – Nithish Albin Apr 03 '19 at 06:23
0

It is the difference if You want to export to PDF or PNG.

In the case of PDF, in my opinion, the best way is to use a virtual plotter for example PDFCreator. because You need to set paper size and so one.

But for .PNG You may use command _Export. Default it will show window save dialogue, but if You set system variable FILEDIA to 0, the dialogue will be disabled and You may export using just command line. Just remember to set FILEDIA to old value at the end. in another case it makes You very unhappy ;)

CAD Developer
  • 1,532
  • 2
  • 21
  • 27