Author Topic: Dimension Stuff  (Read 1322 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1900
  • class keyThumper<T>:ILazy<T>
Dimension Stuff
« on: September 12, 2023, 11:14:01 PM »
I need to do some research into Dimension Styles using .NET API, so I thought I'd drop some code in here.

I'll probably add to this thread as I proceed.

Comments and test results will be appreciated.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1900
  • class keyThumper<T>:ILazy<T>
Re: Dimension Stuff
« Reply #1 on: September 12, 2023, 11:16:16 PM »
Notes:

Quote

A new dimension style is created by creating an instance of a DimStyleTableRecord object and
   then adding it to the DimStyleTable with the Add method.
   Before the dimension style is added to the table, set the name of the new style with the Name property.

You can also copy an existing style or a style with overrides.
   Use the CopyFrom method to copy a dimension style from a source object to a dimension style.
   The source object can be another DimStyleTableRecord object, a Dimension, Tolerance,
   or Leader object, or even a Database object.

If you copy the style settings from another
   dimension style, the current style is duplicated exactly.

If you copy the style settings from
   a Dimension, Tolerance, or Leader object, the current settings,
   including any object overrides, are copied to the style.

If you copy the current style of a Database object,
   the dimension style plus any drawing overrides are copied to the new style.


TESTSTYLE$0   Linear child of TESTSTYLE (both rotated and aligned types)
TESTSTYLE$2   Angular child of TESTSTYLE (both 2-line and 3-point types)
TESTSTYLE$3   Diameter child of TESTSTYLE
TESTSTYLE$4   Radius child of TESTSTYLE
TESTSTYLE$6   Ordinate child of TESTSTYLE
TESTSTYLE$7   Leader child of TESTSTYLE (used for tolerance entities (AcDbFcf) also)

T.B.C.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1900
  • class keyThumper<T>:ILazy<T>
Re: Dimension Stuff
« Reply #2 on: September 12, 2023, 11:22:02 PM »
Import DimStyleTableRecords ; either by name or all.

Code - C#: [Select]
  1. /// <summary>
  2. /// Import Dimension StyleRecords, either all or nominated names.
  3. /// </summary>
  4. /// <param name="sourceCADFilePath">Fully qualified file name, either .DWG or .DXF</param>
  5. /// <param name="styleNames">List of StyleNames OR null for all. </param>
  6. /// <returns></returns>
  7. internal static bool ImportDimensionStyleFromFile(string sourceCADFilePath, List<string> styleNames = null)
  8. {
  9.    var doc = CadApp.DocumentManager.MdiActiveDocument;
  10.    var db = doc.Database;
  11.  
  12.    if (!File.Exists(sourceCADFilePath) )
  13.    { doc.Editor.WriteMessage($"File Exists Failure for {sourceCADFilePath}");
  14.       return false;
  15.    }
  16.    //else, proceed
  17.  
  18.    using (var sourceDb = new Database(false, true))
  19.    {
  20.       ObjectIdCollection idsForAction = new ObjectIdCollection();
  21.  
  22.       string fileExtension = System.IO.Path.GetExtension(sourceCADFilePath);
  23.       string fileName = System.IO.Path.GetFileNameWithoutExtension(sourceCADFilePath);
  24.  
  25.       try
  26.       {
  27.          if (fileExtension == ".dxf")
  28.             sourceDb.DxfIn(sourceCADFilePath, null);
  29.          else if (fileExtension == ".dwg")
  30.             sourceDb.ReadDwgFile(sourceCADFilePath,
  31.                FileOpenMode.OpenForReadAndReadShare,
  32.                false, null);
  33.          else
  34.          {
  35.             return false;
  36.          }
  37.       }
  38.       catch (System.Exception)
  39.       {
  40.          return false;
  41.       }
  42.  
  43.  
  44.       using (Transaction tr = db.TransactionManager.StartTransaction())
  45.       {
  46.          DimStyleTable dimstyleTable = (DimStyleTable)tr.GetObject(
  47.             db.DimStyleTableId, OpenMode.ForWrite);
  48.  
  49.          using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction())
  50.          {
  51.             DimStyleTable sourceDimStyleTable = (DimStyleTable)sourceTr.GetObject(
  52.                sourceDb.DimStyleTableId, OpenMode.ForRead);
  53.  
  54.  
  55.             // either nominated name or all, providing it exists in source an not in target.
  56.             foreach (ObjectId id in sourceDimStyleTable)
  57.             {
  58.                DimStyleTableRecord sourceStyleRecord = (DimStyleTableRecord)sourceTr.GetObject(
  59.                   id, OpenMode.ForRead);
  60.  
  61.                if (!(sourceStyleRecord is null)
  62.                      && (styleNames is null || styleNames.Contains(sourceStyleRecord.Name))
  63.                      && !(dimstyleTable.Has(sourceStyleRecord.Name)))
  64.                {
  65.                   idsForAction.Add(id);
  66.                }
  67.             }
  68.          }
  69.          if (idsForAction.Count != 0)
  70.          {
  71.             IdMapping iMap = new IdMapping();
  72.             db.WblockCloneObjects(idsForAction,
  73.                db.DimStyleTableId,
  74.                iMap,
  75.                DuplicateRecordCloning.Ignore,
  76.                false);
  77.          }
  78.          tr.Commit();
  79.       }
  80.    }
  81.    return true;
  82. }
  83.  


Testing:

Code - C#: [Select]
  1. [CommandMethod("ImportDimStyle_2")]
  2. public static void ImportDimStyle_2()
  3. {
  4.    string templateFileName = @"C:\SD2023Tools\KdubNZ\Templates\CTT_2022.dwg";
  5.    var stylesToImport = new List<string> { "STD35", "STD35A", "ANNO35", "ANNO35$2", "ANNO35A" };
  6.  
  7.    var result = ImportDimensionStyleFromFile(templateFileName, stylesToImport);
  8.  
  9.    Current.WriteMessage($"Result was : {result}");
  10. }
  11.  
  12. [CommandMethod("ImportDimStyle_1")]
  13. public static void ImportDimStyle_1()
  14. {
  15.    string templateFileName = @"C:\SD2023Tools\KdubNZ\Templates\CTT_2022.dwg";
  16.    var result = ImportDimensionStyleFromFile(templateFileName);
  17.  
  18.    Current.WriteMessage($"Result was : {result}");
  19. }
  20.  
  21.  

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1900
  • class keyThumper<T>:ILazy<T>
Re: Dimension Stuff
« Reply #3 on: September 13, 2023, 01:08:43 AM »
The danger with doing research is staying targeted.

I was reviewing the 'ImportDimensionStyleFromFile' code and had the thought ;

It would probably only take couple hours to build one method that could provide the same functionality for all nine dataTables.
. . . . and couple more to test


Code - C#: [Select]
  1.       public static <ToBeDetermined>  ImportSymbolTableRecords<T>(
  2.             this Database targetDb,
  3.             string sourceCADFilePath,
  4.             List<string> recordNamesToImport = null)
  5.             where T : SymbolTable
  6.       {
  7.          // bla,bla, bla
  8.          // do some magic here
  9.  
  10.  
  11.          return returnIt;
  12.       }
  13.  
  14.  


might play next week :)
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8428
  • AKA Daniel
Re: Dimension Stuff
« Reply #4 on: September 13, 2023, 02:13:10 AM »
Thatís cool!

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1900
  • class keyThumper<T>:ILazy<T>
Re: Dimension Stuff
« Reply #5 on: September 13, 2023, 06:10:07 AM »
Code - C#: [Select]
  1. public static ObjectId MakeDimensionStyleCurrent(this Database db, string dimensionStyleName)
  2. {
  3.    using (var tr = db.TransactionManager.TopTransaction)
  4.    {
  5.       DimStyleTable dimensionTable = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
  6.       ObjectId namedDimensionIdId = ObjectId.Null;
  7.  
  8.       if (dimensionTable.Has(dimensionStyleName))
  9.          namedDimensionIdId = dimensionTable[dimensionStyleName];
  10.       else
  11.       {
  12.          Current.WriteMessage($"\nNeed to create DimensionStyle '{dimensionStyleName}'");
  13.          return ObjectId.Null;
  14.       }
  15.  
  16.       DimStyleTableRecord dimensionRecord = (DimStyleTableRecord)tr.GetObject(namedDimensionIdId, OpenMode.ForRead);
  17.  
  18.       // return original definition without any current database over-rides
  19.       // similar to default action of DIMSTYLE dialog option 'Set Current'    
  20.       db.Dimstyle = dimensionRecord.ObjectId;
  21.       db.SetDimstyleData(dimensionRecord);
  22.  
  23.       return dimensionRecord.ObjectId;
  24.    }
  25. }
  26.  

Testing:

Code - C#: [Select]
  1. [CommandMethod("TestMakeDimensionStyleCurrent")]
  2. public static void TestMakeDimensionStyleCurrent()
  3. {
  4.    // (getvar "DIMSTYLE")
  5.    var db = Current.Database;
  6.    using (var tr = Current.Transaction(db))
  7.    {
  8.       string dimensionStyleName = "STD35";
  9.       var returnedId = db.MakeDimensionStyleCurrent(dimensionStyleName);
  10.  
  11.       Current.WriteMessage($"\n'returnedId' for {dimensionStyleName} is : {(returnedId.IsNull ? "Null" : "OK")}\n");
  12.  
  13.  
  14.       dimensionStyleName = "licorice";
  15.       returnedId = db.MakeDimensionStyleCurrent(dimensionStyleName);
  16.  
  17.       Current.WriteMessage($"\n'returnedId' for {dimensionStyleName} is : {(returnedId.IsNull ? "Null" : "OK")}\n");
  18.       tr.Commit();
  19.    }
  20. }
  21.  


Library

Code - C#: [Select]
  1.    public static class Current
  2.    {
  3.       public static Document Document => CadApp.DocumentManager.MdiActiveDocument;
  4.  
  5.       public static Database Database => Document.Database;
  6.  
  7.       public static Editor Editor => Document.Editor;
  8.  
  9.       public static void WriteMessage(string message)
  10.       {
  11.          Editor.WriteMessage(message);
  12.       }
  13.  
  14.       public static void WriteMessage(string message, params object[] parameter)
  15.       {
  16.          Editor.WriteMessage(message, parameter);
  17.       }
  18.  
  19.       public static Transaction Transaction(Database db)
  20.       {
  21.          return db.TransactionManager.StartTransaction();
  22.       }
  23.  
  24.       // . . .
  25.  
  26.       public static Transaction GetTopTransaction(this Database db)
  27.       {
  28.          AcGuard.IsNotNull(db, "db");
  29.          Transaction topTransaction = db.TransactionManager.TopTransaction;
  30.          if (!(topTransaction == null))
  31.          {
  32.             return topTransaction;
  33.          }
  34.  
  35.          throw new Exception(ErrorStatus.NoActiveTransactions,
  36.                                          "non-continuable error in 'GetTopTransaction' ");
  37.       }
  38. }
  39.  
  40.  
  41.  
  42.  
  43.    public static class AcGuard
  44.    {
  45.       public static void IsNotNullObjectId(ObjectId id, string paramName)
  46.       {
  47.          if (id.IsNull)
  48.          {
  49.             throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId, paramName);
  50.          }
  51.       }
  52.  
  53.       public static void IsNotNull<T>(T obj, string paramName) where T : class
  54.       {
  55.          if (obj == null)
  56.          {
  57.             throw new ArgumentNullException(paramName);
  58.          }
  59.       }
  60.  
  61.       public static void IsNotNullOrWhiteSpace(string str, string paramName)
  62.       {
  63.          if (string.IsNullOrWhiteSpace(str))
  64.          {
  65.             throw new ArgumentException("eNullOrWhiteSpace", paramName);
  66.          }
  67.       }
  68.  
  69.       // . . .
  70.    }
  71.  
  72.  
  73.  
« Last Edit: September 13, 2023, 06:19:21 AM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.