Ad

Tuesday, October 17, 2017

Style Changes

Have you ever wanted to change styles quickly? Maybe you want go from OldStyle to NewStyle, but don’t want to do it manually. Well here is maybe one way to quickly change styles for lots of objects using a LispFunction. A LispFuntion allows Lisp to talk to .NET.

In this case we can use fancy inheritance to set the style name of any object type we want. We do this by using the fact that most Civil 3D objects are a type of Autodesk.Civil.DatabaseServices.Entity which contains the StyleName property. Luckily this property takes a string and Civil 3D takes care of finding the correct style to use based on the name given. If the style isn’t in the drawing an exception will occur and an error message sent to the command line.

After loading the dll you can then type at the command line:

(ChangeCivil3DStyleByName “AECC_ALIGNMENT” “OldStyleName” “NewStyleName”)

Then press enter. Then each alignment with the OldStyleName will be magically changed to the NewStyleName. If you have lots of objects to change I’d build a list in Excel with the appropriate object type and style names and use the Concatenate formula to build the above lines. Then you can add it to a lisp or copy and paste at the command line.

To get the object type names (like AECC_ALIGNMENT), select the object and then type List at the command line. The first line should give you the value.

public class StyleChanges

{

     [LispFunction("ChangeCivil3DStyleByName")]

     public void ChangeStyleName(ResultBuffer rbArgs)

     {

          if (rbArgs == null) {

          Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nNo objects passed");

     }

     var rbArgsList = rbArgs.AsArray().ToList();

     if (rbArgsList.Count() != 3)

     {

          Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nNeed three objects to be passed.");

     }    

     try

     {

          var civDoc = CivilApplication.ActiveDocument;

          var objectType = rbArgsList[0].Value.ToString();

          var oldStyleName = rbArgsList[1].Value.ToString();

          var newStyleName = rbArgsList[2].Value.ToString();

          // Aec.DatabaseServices.Entity

          var doc = Application.DocumentManager.MdiActiveDocument;

          var ed = doc.Editor;

          using (var tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())

          {

               // Need to get the objects

               TypedValue[] vals = new TypedValue[]

               {

                    new TypedValue((int)DxfCode.Start, objectType)

               };

               var res = ed.SelectAll(new SelectionFilter(vals));

               if (res.Status == PromptStatus.OK) { foreach (ObjectId objId in res.Value.GetObjectIds())

               {

                    var aecObj = objId.GetObject(OpenMode.ForRead) as Autodesk.Civil.DatabaseServices.Entity;

                    if (aecObj.StyleName == oldStyleName)

                    {

                         aecObj.UpgradeOpen();

                         aecObj.StyleName = newStyleName;

                    }

               }

         }

    tr.Commit();

}

}

catch (System.Exception ex) { Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nError: " + ex.Message); } } }

LinkWithin

Blog Widget by LinkWithin

Ad