4

I have developed an external WPF application to generate drawings in c#. I have been able to draw, dimension, add blocks and every thing else required by the application using Autodesk.AutoCAD.Interop, however I can't seem to populate The title block, or generate a parts list.

All the examples I've seen are based on the mechanism that requires the application to run as a plugin inside AutoCAD. The truth is, inserting a line used is one or two lines of code using ModelSpace.InsertLine, now, it's at least 8 lines of code!

Is there a way to achieve this functionality using the Autodesk.AutoCAD.Interop? Or is there a way to combine using the interop with a plugin that can be called from the external exe?

Any pointers on this will be appreciated.

Thanks.

EDIT To illustrate:

// before - Draw Line with Autodesk.AutoCAD.Interop
private static AcadLine DrawLine(double[] startPoint, double[] endPoint)
{
    AcadLine line = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint);
    return line;
}
// Now - Draw line with Autodesk.AutoCAD.Runtime
[CommandMethod("DrawLine")]
public static Line DrawLine(Coordinate start, Coordinate end)
{
    // Get the current document and database 
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

        // Create a line that starts at 5,5 and ends at 12,3
        Line acLine = new Line(start.Point3d, end.Point3d);

        acLine.SetDatabaseDefaults();

        // Add the new object to the block table record and the transaction
        acBlkTblRec.AppendEntity(acLine);
        acTrans.AddNewlyCreatedDBObject(acLine, true);

        // Save the new object to the database
        acTrans.Commit();
        return acLine;
    }
}
reckface
  • 5,678
  • 4
  • 36
  • 62
  • It's worth noting that even though AutoCAD internal development using AcMdg.dll and AcDbMdg.dll is typically more involved, it's highly efficient when compared to it's Interop counterparts. – Parrish Husband Oct 10 '13 at 09:47
  • @reckface - Can you please let me know that how are you sending commands to AutoCad without opening it explicitly. Can it be done via pure .Net code? – Rohit Vats Nov 27 '15 at 15:59
  • I have a wrapper class appropriately called CAD. I call CAD.Setup(), which creates a new AutoCAD.Application if it hasn't been created, then app.Documents.Add or create, and from there on the CAD class wraps all my calls to the document. It's a pure .Net exe installs with ClickOnce deployment. – reckface Nov 30 '15 at 15:37

1 Answers1

7

Yes, you can absolutely combine the two approaches.

  1. Write an in-process DLL that does the work in AutoCAD. Make the commands you wish to call available to the command line by flagging your public methods with [CommandMethod("MethodName")].

  2. Get AutoCAD started or connected via interop calls.

  3. Using the interop AcadApplication, netload your DLL, and then call your work functions from the command line.

*Bonus * You can pass interop parameters to internal commands much easier this way too.

Here's an example of how you could build a commandmethod in-process and then call it via COM:

[CommandMethod("EditBlockAtt")]
public void EditBlockAtt()
{
    var acDb = HostApplicationServices.WorkingDatabase;
    var acEd = AcadApplication.DocumentManager.MdiActiveDocument.Editor;

    var blockNamePrompt = acEd.GetString(Environment.NewLine + "Enter block name: ");
    if (blockNamePrompt.Status != PromptStatus.OK) return;
    var blockName = blockNamePrompt.StringResult;

    var attNamePrompt = acEd.GetString(Environment.NewLine + "Enter attribute name: ");
    if (attNamePrompt.Status != PromptStatus.OK) return;
    var attName = attNamePrompt.StringResult;

    var acPo = new PromptStringOptions(Environment.NewLine + "Enter new attribute value: "){ AllowSpaces = true };
    var newValuePrompt = acEd.GetString(acPo);
    if (newValuePrompt.Status != PromptStatus.OK) return;
    var newValue = newValuePrompt.StringResult;

    using (var acTrans = acDb.TransactionManager.StartTransaction())
    {
        var acBlockTable = acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable;
        if (acBlockTable == null) return;

        var acBlockTableRecord = acTrans.GetObject(acBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
        if (acBlockTableRecord == null) return;

        foreach (var blkId in acBlockTableRecord)
        {
            var acBlock = acTrans.GetObject(blkId, OpenMode.ForRead) as BlockReference;
            if (acBlock == null) continue;
            if (!acBlock.Name.Equals(blockName, StringComparison.CurrentCultureIgnoreCase)) continue;
            foreach (ObjectId attId in acBlock.AttributeCollection)
            {
                var acAtt = acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;
                if (acAtt == null) continue;

                if (!acAtt.Tag.Equals(attName, StringComparison.CurrentCultureIgnoreCase)) continue;
                    
                acAtt.UpgradeOpen();
                acAtt.TextString = newValue;
            }
        }

        acTrans.Commit();
    }
}

Then from an interop AcadApplication, netload the dll and call the method from the commandline in this format:

(Command "EditBlockAtt" "BlockName" "AttributeName" "NewValue")

However if you want to go pure Interop, this may get you what you need given you have an AcadDocument object at runtime:

foreach (AcadEntity ent in acadDoc.ModelSpace)
{
    var block = ent as AcadBlockReference;
    if (block == null) continue;
    {
        if (!block.Name.Equals("BlockName", StringComparison.CurrentCultureIgnoreCase)) continue;
        var atts = block.GetAttributes() as object[];
        if (atts == null) continue;

        foreach (var attribute in atts.OfType<AcadAttributeReference>()
            .Where(attribute => attribute.TagString.Equals("AttributeName", 
                                StringComparison.CurrentCultureIgnoreCase)))
        {
            attribute.TextString = "New Value";
        }
    }
}

Also note this is using the AutoCAD 2012 Interop libraries. YMMV.

Parrish Husband
  • 3,148
  • 18
  • 40
  • Had to wait until I had an IDE in front of me to extend my answer to include the interop method. If you find this takes a while on larger drawings, it might be worth filtering a selection to just blocks beforehand, and iterating through that instead of the entire model space. – Parrish Husband Oct 10 '13 at 12:44
  • Thank you. I am considering implementing the new acMdg.dll .net references, but deadline constraints mean I have to produce something now, before devoting more time to better understanding the interface. I stripped everything down yesterday to acMdg.dll and found out you can't run it standalone, and I had to quickly backtrack to the interop! Thanks again, I will work on combining both approaches. – reckface Oct 10 '13 at 22:02
  • @Parrish - Can you please let me know that how are you sending commands to AutoCad without opening it explicitly. Can it be done via pure .Net code? – Rohit Vats Nov 27 '15 at 15:59
  • @RohitVats, in-process .NET calls to AutoCAD assumes a running application. However you can instantiate AutoCAD via Interop, and then send in-process commands to the newly running instance. What exactly are you aiming for? – Parrish Husband Nov 27 '15 at 16:04
  • I am writing a software that will load dwg file, pick some blocks from it, modify it (set some attributes etc), explode it and save it in new dwg file. But right now I am struggling with opening AutoCAD from process itself. It's failing always with some COM exception. :( I have tried both the approaches metioned here - http://stackoverflow.com/questions/24389965/how-can-i-open-autocad-2015-through-the-net-api but none of them working for me. (Tried via marshalling and also tried creating instance of AcadApplication) – Rohit Vats Nov 27 '15 at 16:13
  • @ParrishHusband - I have posted my question here - http://stackoverflow.com/questions/33961411/launch-autocad-2015-from-net-process. Any help will be greatly appreciated.. :) – Rohit Vats Nov 27 '15 at 16:25