Amend AutoCAD from Revit

Is it possible to write some Dynamo or Python that will run in Revit using either Dynamo or Dynamo Player that will alter an AutoCAD file?

For example… let’s say I want to explode all blocks in AutoCAD - can I do that using Dynamo in Revit?

You cannot modify a dwg within revit, you have to open it in Autocad and do that change.

So you can’t access the AutoCAD API via Dynamo in Revit?

You can via com but you do need to have autocad open etc etc.

This may help to guide you on how to use it - GitHub - Autodesk/civilconnection: CivilConnection enables the exchange of information between Civil 3D, Dynamo and Revit.

Architects aren’t allowed Civil3D :frowning:

I have Revit and bog standard AutoCAD.

I found a site that said I could write it in Python but fell at the first hurdle… (pyautocad)

Sorry I meant you can see the code used for civil3d version, and autocad would be similar since civils 3d is built on top of autocad.

2 Likes

Ah.
I found Link DWG thanks to this thread: Is there any way to load Dynamo in Vanilla AutoCAD?

So am trying that.

Personally I hate AutoCAD… But we use it a lot for base plans so I thought maybe I’d give some automation a go.

@Alien
Use brimohareb pak. I accsess Autocad and C3d by using com API

2 Likes

Will that enable me to explode a block or purge?

Why not? If they have a subscription to the AEC collection (shockingly common) it’s built into the costs you’re already paying, so you might as well get your money’s worth. Certainly easier to build Dynamo automations than learn a whole new tool for a single use workflow.

An alternative would be to learn how to write and record a macro in AutoCAD which does the work for you, and how to send a command from Dynamo for Revit to the active AutoCAD instance to run that command.

The easy bit: there is Python on the forum to do the later part of this.

The harder bit: This forum isn’t the right platform to learn the specifics of former part, but it is simple enough that you can likely manage it with some basic instructions. First focus on doing everything from just the keyboard, then use those keyboard actions in sequence during a macro recording session, and save the resulting macro. When done you’ll have a command to do the work for you.

1 Like

I’m saying they won’t let us loose with anything other than Revit and AutoCAD because we can’t be trusted :rofl:

That package you suggested in the other thread works fairly well…
The bits that aren’t working I adjusted the Python.
I made some progress. :upside_down_face:

1 Like

A few corrections…

  • Yes you can work with DWGs from Revit
  • No you don’t need Civil3D -or- AutoCAD at all

Revit comes with AcDbMgd.dll installed (Shh don’t tell anyone)
Minimal code here

import clr
from System.IO import FileShare

clr.AddReference("AcDbMgd")
from Autodesk.AutoCAD.DatabaseServices import *

dwgfile = IN[0]  # Path to dwg

with Database(False, True) as db:
    db.ReadDwgFile(dwgfile, FileShare.ReadWrite, False, "")
    with db.TransactionManager.StartTransaction() as t:
        # Do some AutoCAD API stuff here
        t.Commit()
    db.SaveAs(dwgfile, DwgVersion.Current)

I didn’t get time this year but I was thinking of putting together a presentation for AU titled I can’t believe it’s not AutoCAD on how to swap out a title block and update attributes from Revit

6 Likes

This is known, but last I checked it also doesn’t have a lot of the other tools required for safely editing existing DWGs which aren’t in the Revit application (hence the recommendation). It may work for exploding all blocks though… certainly worth a comparison between the two though.

1 Like

I’ve used it extensively to post-process and clean up DWGs from Revit and have been able to achieve most requests from colleagues and clients. I have come across limitations with parts of the API that depend on a running AutoCAD instance, however there is usually a workaround. However it has taken far longer to learn the idiosyncrasies and esoteric corners of the AutoCAD API than Revit. And yes it is much easier to crash Revit or C3D with it

Basically my company works with a lot of existing buildings so we nearly always get a CAD floor plan or ten…
And every one has to be cleaned up (or at least that’s what we’re meant to do) before putting it into the Revit model.
I really dislike CAD but I thought maybe I’d try and make the Luddites a little more excited about Dynamo if I could automate the clean up.

So far even the BIM team aren’t so enthused by my ‘radical’ suggestions on how to do this… Shame on you when you read this guys :stuck_out_tongue:

I keep getting this error:

SystemError: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I get it trying to use the active document or with filepath when document is closed.

Trying to unlock the layers
layerTable = trans.GetObject(db.LayerTableId, OpenMode.ForWrite)

First rule of AutoCAD API is don’t open for write anything that is not being altered, start with OpenMode.ForRead and then iff you need to modify, alter, erase etc. use Object.UpgradeOpen()
If you’re working in C3D you need to lock the application document before touching it - the Dynamo python C3D boilerplate is a good start.
Here’s a snippet (inside transaction)

layertable = t.GetObject(db.LayerTableId, OpenMode.ForRead)
for layerId in layertable:
    layerObj = t.GetObject(layerId, OpenMode.ForRead)
    if layerObj.IsLocked:
        layerObj.UpgradeOpen()
        layerObj.IsLocked = False
2 Likes

Yep - the access method while it can work, isn’t really built to be used this way, but it’s utility is certainly there for those who want to fight it. Not that I am not sure about the license compliance of such applications, so I do not recommend this for commercial work.

What do you mean?

Are you saying if I use the code to alter some AutoCAD I’m breaking a licence agreement?

Only working with Revit and AutoCAD.

I get this if the AutoCAD drawing is open or closed.