AutoCAD P&ID/Plant 3D: Is there any API to recalculate Tags?

By Marat Mirgaleev

Issue

When a tag format changes, manual update of the respective tags is a very time consuming operation. Can I update the tags with the API?

Solution

To update a tag, you can simply write out a blank tag value with the DataLinksManager.SetProperties() which causes a recalc.

Here is a sample how to do this. It works for Piping and P&ID.

[CommandMethod("PexTagUpdate", CommandFlags.Modal)]    public void PexTagUpdate() // This method can have any name    {      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;      ed.WriteMessage("nUpdate tag values");      string dwgtype = PnPProjectUtils.GetActiveDocumentType();           if (dwgtype != "PnId" && dwgtype != "Piping")      {  //"PnId", "Piping", "Ortho", "Iso"        ed.WriteMessage("nDrawing must be a PnId or Piping drawing"                      + " in the current project.n");        return;      }      Project prj = PnPProjectUtils.GetProjectPartForCurrentDocument();      DataLinksManager dlm = prj.DataLinksManager;           PromptSelectionOptions pso = new PromptSelectionOptions();      pso.MessageForAdding = "nSelect " + dwgtype                            + " objects to update : ";      PromptSelectionResult selResult = ed.GetSelection(pso);      if (selResult.Status == PromptStatus.Cancel)         return;      if (selResult.Status != PromptStatus.OK)         selResult = ed.SelectAll();      SelectionSet ss = selResult.Value;      ObjectId[] objIds = ss.GetObjectIds();      int nTags = 0, nTagsModified = 0;           foreach (ObjectId objId in objIds)      {        Database oDB =                    Application.DocumentManager.MdiActiveDocument.Database;        Autode
sk.AutoCAD.DatabaseServices.TransactionManager tmgr           = oDB.TransactionManager;        using (Transaction t = tmgr.StartTransaction())        {          StringCollection strNames = new StringCollection();          StringCollection strNewValues = new StringCollection();          strNames.Add("Tag");          strNewValues.Add("");  // A blank value causes a recalc.                      // A tag that is currently blank will remain blank.          try          {            DBObject obj = t.GetObject(objId, OpenMode.ForRead);            if (!dlm.HasLinks(objId))               continue; // Not a Plant object            nTags++;            StringCollection strOldValues =                 dlm.GetProperties(objId, strNames, true);                 //----- HERE IT IS. UPDATE OF THE TAG ------------------------            dlm.SetProperties(objId, strNames, strNewValues);            //------------------------------------------------------------                 t.Commit();                 // Get new values            StringCollection strRecalcValues =                 dlm.GetProperties(objId, strNames, true);            if (strRecalcValues[0] == "")               continue;            nTagsModified++;            // Report changes            ed.WriteMessage("n   tag=" + strRecalcValues[0]);            if (strOldValues[0] != strRecalcValues[0])               ed.WriteMessage("  oldtag=" + strOldValues[0]);          }          catch (System.Exception e)          {            if (t != null
)            {              t.Abort();              t.Dispose();            }            ed.WriteMessage("nCould not update tag: "                           + e.Message.ToString());          }        }      }      ed.WriteMessage("nDone (" + nTagsModified + " of " + nTags                     + " modified).");    }

Comments

4 responses to “AutoCAD P&ID/Plant 3D: Is there any API to recalculate Tags?”

  1. Surely, it will be better to move StartTransaction() out of the foreach cycle.

  2. Klaus Schachinger Avatar
    Klaus Schachinger

    is there a way which normal user can do?

  3. It’s very useful code! I can’t understand why Plant 3D developers don’t realize this command as standart.
    Thank you!

  4. Klaus Schachinger Avatar
    Klaus Schachinger

    Hi, I am still waiting for that command!

Leave a Reply to Klaus SchachingerCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading