Author Topic: Screw it I give up  (Read 10467 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: Screw it I give up
« Reply #15 on: December 02, 2014, 12:36:38 PM »
I created a C++ project for me to start coding some of my ideas. Right now, it is a command line utility (run by double clicking .bat files) but I do plan on creating a GUI for it once I get past some of the road blocks I foresee.

So far most of what I have done is "project setup" (build files, tests, documentation, etc.). However I do have some code already though; I took some code from some other projects. As it stands now, what builds is an EXE, two DLLs and three test cases. All three tests technically fail at this point but I will let you all know when I get them working and a useful distribution.

The project is being hosted on Visual Studio Online (I plan to support building with Clang, MinGW and Visual Studio compilers) so that means the Visual Studio Project Solution will be a supplementary file (used for building with VS only) and I will be coding this the Unix way (Text editor and CMakeList files). Invites to join the project in a bit (I will need a lot of help later). I also plan on getting online builds running for this project (I really want to test this feature out).

The name of this project is: "JaSP" and it stands for (I thought this was funny): "Just a Simple Project". I choose the name because this project, for me, is getting BIG fast and it will/should be able to create a project from simple to complicated based on the `recipe' used.

:)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #16 on: December 04, 2014, 01:14:04 PM »
Sorry default project in  link of html file in first post was all screwed up and no idea what happened but updated so it already downloaded just re-download.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #17 on: December 04, 2014, 02:37:04 PM »
Here is default project to download directly

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Screw it I give up
« Reply #18 on: December 07, 2014, 04:32:01 PM »
Briefly because OFF-TOPIC! html-formatted code copy is part of the Productivity Power Tools Visual Studio plugin. That plugin has lots of cool things BTW.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #19 on: August 01, 2015, 12:19:40 AM »
Database

Code - C#: [Select]
  1. using Autodesk.AutoCAD.Runtime;
  2. using System;
  3. namespace Autodesk.AutoCAD.DatabaseServices
  4. {
  5.     public static class DatabaseExtensions
  6.     {
  7.         private static string standard = "Standard";
  8.  
  9.         public static BlockTable BlockTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  10.         {
  11.             if (trx == null)
  12.             {
  13.                 throw new NoActiveTransactionException("No active Transaction");
  14.             }
  15.              return (BlockTable)trx.GetObject(db.BlockTableId, mode, false, false);
  16.         }
  17.  
  18.  
  19.         public static BlockTable BlockTable(this Database db, OpenMode mode = OpenMode.ForRead)
  20.         {
  21.             return db.BlockTable(db.TransactionManager.TopTransaction, mode);
  22.         }
  23.  
  24.  
  25.         public static DimStyleTable DimStyleTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  26.         {
  27.             if (trx == null)
  28.             {
  29.                 throw new NoActiveTransactionException("No active Transaction");
  30.             }
  31.             return (DimStyleTable)trx.GetObject(db.DimStyleTableId, mode, false, false);
  32.         }
  33.  
  34.  
  35.         public static DimStyleTable DimStyleTable(this Database db, OpenMode mode = OpenMode.ForRead)
  36.         {
  37.             return db.DimStyleTable(db.TransactionManager.TopTransaction, mode);
  38.         }
  39.  
  40.  
  41.  
  42.         public static LayerTable LayerTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  43.         {
  44.             if (trx == null)
  45.             {
  46.                 throw new NoActiveTransactionException("No active Transaction");
  47.             }
  48.             return (LayerTable)trx.GetObject(db.LayerTableId, mode, false, false);
  49.         }
  50.  
  51.  
  52.         public static LayerTable LayerTable(this Database db, OpenMode mode = OpenMode.ForRead)
  53.         {
  54.             return db.LayerTable(db.TransactionManager.TopTransaction, mode);
  55.         }
  56.  
  57.         public static LinetypeTable LinetypeTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  58.         {
  59.             if (trx == null)
  60.             {
  61.                 throw new NoActiveTransactionException("No active Transaction");
  62.             }
  63.             return (LinetypeTable)trx.GetObject(db.LinetypeTableId, mode, false, false);
  64.         }
  65.  
  66.  
  67.         public static LinetypeTable LinetypeTable(this Database db, OpenMode mode = OpenMode.ForRead)
  68.         {
  69.             return db.LinetypeTable(db.TransactionManager.TopTransaction, mode);
  70.         }
  71.  
  72.  
  73.  
  74.         public static RegAppTable RegAppTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  75.         {
  76.             if (trx == null)
  77.             {
  78.                 throw new NoActiveTransactionException("No active Transaction");
  79.             }
  80.             return (RegAppTable)trx.GetObject(db.RegAppTableId, mode, false, false);
  81.         }
  82.  
  83.  
  84.         public static RegAppTable RegAppTable(this Database db, OpenMode mode = OpenMode.ForRead)
  85.         {
  86.             return db.RegAppTable(db.TransactionManager.TopTransaction, mode);
  87.         }
  88.  
  89.  
  90.  
  91.         public static TextStyleTable TextStyleTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  92.         {
  93.             if (trx == null)
  94.             {
  95.                 throw new NoActiveTransactionException("No active Transaction");
  96.             }
  97.             return (TextStyleTable)trx.GetObject(db.TextStyleTableId, mode, false, false);
  98.         }
  99.  
  100.  
  101.         public static TextStyleTable TextStyleTable(this Database db, OpenMode mode = OpenMode.ForRead)
  102.         {
  103.             return db.TextStyleTable(db.TransactionManager.TopTransaction, mode);
  104.         }
  105.  
  106.         public static ObjectId StandardTextStyle(this Database db)
  107.         {
  108.           return  SymbolUtilityServices.GetTextStyleStandardId(db);
  109.         }
  110.  
  111.  
  112.         public static ObjectId ContinuousLinetypeId(this Database db)
  113.         {
  114.             return SymbolUtilityServices.GetLinetypeContinuousId(db);
  115.         }
  116.  
  117.         public static ObjectId StandardDimStyle(this Database db, Transaction trx)
  118.         {
  119.             var dimtbl = db.DimStyleTable(trx);
  120.             return dimtbl.Has(standard) ? dimtbl[standard] : db.Dimstyle;
  121.         }
  122.  
  123.         public static ObjectId StandardDimStyle(this Database db)
  124.         {
  125.             return db.StandardDimStyle(db.TransactionManager.TopTransaction);
  126.         }
  127.  
  128.  
  129.  
  130.         public static UcsTable UcsTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  131.         {
  132.             if (trx == null)
  133.             {
  134.                 throw new NoActiveTransactionException("No active Transaction");
  135.             }
  136.             return (UcsTable)trx.GetObject(db.UcsTableId, mode, false, false);
  137.         }
  138.  
  139.  
  140.         public static UcsTable UcsTable(this Database db, OpenMode mode = OpenMode.ForRead)
  141.         {
  142.             return db.UcsTable(db.TransactionManager.TopTransaction, mode);
  143.         }
  144.  
  145.  
  146.  
  147.         public static ViewportTable ViewportTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  148.         {
  149.             if (trx == null)
  150.             {
  151.                 throw new NoActiveTransactionException("No active Transaction");
  152.             }
  153.             return (ViewportTable)trx.GetObject(db.ViewportTableId, mode, false, false);
  154.         }
  155.  
  156.  
  157.         public static ViewportTable ViewportTable(this Database db, OpenMode mode = OpenMode.ForRead)
  158.         {
  159.             return db.ViewportTable(db.TransactionManager.TopTransaction, mode);
  160.         }
  161.  
  162.  
  163.  
  164.         public static ViewTable ViewTable(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  165.         {
  166.             if (trx == null)
  167.             {
  168.                 throw new NoActiveTransactionException("No active Transaction");
  169.             }
  170.             return (ViewTable)trx.GetObject(db.ViewTableId, mode, false, false);
  171.         }
  172.  
  173.  
  174.         public static ViewTable ViewTable(this Database db, OpenMode mode = OpenMode.ForRead)
  175.         {
  176.             return db.ViewTable(db.TransactionManager.TopTransaction, mode);
  177.         }
  178.  
  179.         /// <summary>
  180.         /// Author: Tony Tanzillo
  181.         /// Source: http://www.theswamp.org/index.php?topic=41311.msg464457#msg464457
  182.         /// </summary>
  183.         /// <param name="db"></param>
  184.         /// <param name="trx"></param>
  185.         /// <param name="mode"></param>
  186.         /// <returns></returns>
  187.        public static BlockTableRecord ModelSpace(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  188.         {
  189.             if (trx == null)
  190.             {
  191.                 throw new NoActiveTransactionException("No active Transaction");
  192.             }
  193.             return (BlockTableRecord)trx.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),mode, false, false);
  194.         }
  195.  
  196.  
  197.         public static BlockTableRecord ModelSpace(this Database db, OpenMode mode = OpenMode.ForRead)
  198.         {
  199.             return db.ModelSpace(db.TransactionManager.TopTransaction, mode);
  200.         }
  201.  
  202.  
  203.        
  204.  
  205.         /// <summary>
  206.         /// Author: Tony Tanzillo
  207.         /// Source: http://www.theswamp.org/index.php?topic=41311.msg464457#msg464457
  208.         /// </summary>
  209.         /// <param name="db"></param>
  210.         /// <param name="trx"></param>
  211.         /// <param name="mode"></param>
  212.         /// <returns></returns>
  213.         public static BlockTableRecord CurrentSpace(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  214.         {
  215.             if (trx == null)
  216.             {
  217.                 throw new NoActiveTransactionException("No active Transaction");
  218.             }
  219.             return (BlockTableRecord)trx.GetObject(db.CurrentSpaceId, mode, false, false);
  220.         }
  221.  
  222.  
  223.         public static BlockTableRecord CurrentSpace(this Database db, OpenMode mode = OpenMode.ForRead)
  224.         {
  225.             return db.CurrentSpace(db.TransactionManager.TopTransaction, mode);
  226.         }        
  227.  
  228.  
  229.         public static DBDictionary NamedObjectDBDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  230.         {
  231.             if (trx == null)
  232.             {
  233.                 throw new NoActiveTransactionException("No active Transaction");
  234.             }
  235.             return (DBDictionary)trx.GetObject(db.NamedObjectsDictionaryId, mode, false, false);
  236.         }
  237.  
  238.  
  239.         public static DBDictionary NamedObjectDBDictionary(this Database db, OpenMode mode = OpenMode.ForRead)
  240.         {
  241.             return db.NamedObjectDBDictionary(db.TransactionManager.TopTransaction, mode);
  242.         }
  243.  
  244.  
  245.         public static DBDictionary GroupDBDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  246.         {
  247.             if (trx == null)
  248.             {
  249.                 throw new NoActiveTransactionException("No active Transaction");
  250.             }
  251.             return (DBDictionary)trx.GetObject(db.GroupDictionaryId, mode, false, false);
  252.         }
  253.  
  254.  
  255.         public static DBDictionary GroupDBDictionary(this Database db, OpenMode mode = OpenMode.ForRead)
  256.         {
  257.             return db.GroupDBDictionary(db.TransactionManager.TopTransaction, mode);
  258.         }
  259.  
  260.  
  261.         public static DBDictionary MLStyleDBDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  262.         {
  263.             if (trx == null)
  264.             {
  265.                 throw new NoActiveTransactionException("No active Transaction");
  266.             }
  267.             return (DBDictionary)trx.GetObject(db.MLStyleDictionaryId, mode, false, false);
  268.         }
  269.  
  270.  
  271.         public static DBDictionary MLStyleDBDictionary(this Database db, OpenMode mode = OpenMode.ForRead)
  272.         {
  273.             return db.MLStyleDBDictionary(db.TransactionManager.TopTransaction, mode);
  274.         }
  275.  
  276.         public static ObjectId StandardMLStyle(this Database db)
  277.         {
  278.  
  279.             return db.StandardMLStyle(db.TransactionManager.TopTransaction);
  280.         }
  281.  
  282.         public static ObjectId StandardMLStyle(this Database db, Transaction trx)
  283.         {
  284.             var mlDic = db.MLStyleDBDictionary(trx);
  285.             return mlDic.Contains(standard) ? mlDic.GetAt(standard) : db.CmlstyleID;
  286.         }
  287.  
  288.  
  289.         public static DBDictionary LayoutDBDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  290.         {
  291.             if (trx == null)
  292.             {
  293.                 throw new NoActiveTransactionException("No active Transaction");
  294.             }
  295.             return (DBDictionary)trx.GetObject(db.LayoutDictionaryId, mode, false, false);
  296.         }
  297.  
  298.  
  299.         public static DBDictionary LayoutDBDictionary(this Database db, OpenMode mode = OpenMode.ForRead)
  300.         {
  301.             return db.LayoutDBDictionary(db.TransactionManager.TopTransaction, mode);
  302.         }
  303.  
  304.  
  305.         public static DBDictionary PlotSettingsDBDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  306.         {
  307.             if (trx == null)
  308.             {
  309.                 throw new NoActiveTransactionException("No active Transaction");
  310.             }
  311.             return (DBDictionary)trx.GetObject(db.PlotSettingsDictionaryId, mode, false, false);
  312.         }
  313.  
  314.  
  315.         public static DBDictionary PlotSettingsDBDictionary(this Database db, OpenMode mode = OpenMode.ForRead)
  316.         {
  317.             return db.PlotSettingsDBDictionary(db.TransactionManager.TopTransaction, mode);
  318.         }
  319.  
  320.         public static DBDictionary TableStyleDBDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  321.         {
  322.             if (trx == null)
  323.             {
  324.                 throw new NoActiveTransactionException("No active Transaction");
  325.             }
  326.             return (DBDictionary)trx.GetObject(db.TableStyleDictionaryId, mode, false, false);
  327.         }
  328.  
  329.  
  330.         public static DBDictionary TableStyleDBDictionary(this Database db, OpenMode mode = OpenMode.ForRead)
  331.         {
  332.             return db.TableStyleDBDictionary(db.TransactionManager.TopTransaction, mode);
  333.         }
  334.  
  335.  
  336.         public static DBDictionary MLeaderStyleDBDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead)
  337.         {
  338.             if (trx == null)
  339.             {
  340.                 throw new NoActiveTransactionException("No active Transaction");
  341.             }
  342.             return (DBDictionary)trx.GetObject(db.MLeaderStyleDictionaryId, mode, false, false);
  343.         }
  344.  
  345.  
  346.         public static DBDictionary MLeaderStyleDBDictionary(this Database db, OpenMode mode = OpenMode.ForRead)
  347.         {
  348.             return db.MLeaderStyleDBDictionary(db.TransactionManager.TopTransaction, mode);
  349.         }
  350.  
  351.         public static ObjectId StandardMLeaderStyle(this Database db)
  352.         {
  353.  
  354.             return db.StandardMLeaderStyle(db.TransactionManager.TopTransaction);
  355.         }
  356.  
  357.         public static ObjectId StandardMLeaderStyle(this Database db, Transaction trx)
  358.         {
  359.             var mlDic = db.MLeaderStyleDBDictionary(trx);
  360.             return mlDic.Contains(standard) ? mlDic.GetAt(standard) : db.MLeaderstyle;
  361.         }
  362.  
  363.         public static GroupDictionary GroupDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false)
  364.         {
  365.             if (trx == null)
  366.             {
  367.                 throw new NoActiveTransactionException("No active Transaction");
  368.             }
  369.             DBDictionary dic = (DBDictionary)trx.GetObject(db.GroupDictionaryId, mode, false, false);
  370.             return includingErased
  371.                 ? new GroupDictionary(trx, dic, includingErased)
  372.                 : new GroupDictionary(trx, dic.IncludingErased, includingErased);            
  373.         }
  374.  
  375.         public static GroupDictionary GroupDictionary(this Database db, OpenMode mode = OpenMode.ForRead, bool includingErased = false)
  376.         {
  377.             return db.GroupDictionary(db.TransactionManager.TopTransaction, mode, includingErased);
  378.         }
  379.  
  380.         public static MLStyleDictionary MLStyleDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false)
  381.         {
  382.             if (trx == null)
  383.             {
  384.                 throw new NoActiveTransactionException("No active Transaction");
  385.             }
  386.             DBDictionary dic = (DBDictionary)trx.GetObject(db.MLStyleDictionaryId, mode, false, false);
  387.             return includingErased
  388.                 ? new MLStyleDictionary(trx, dic, includingErased)
  389.                 : new MLStyleDictionary(trx, dic.IncludingErased, includingErased);
  390.         }
  391.  
  392.         public static MLStyleDictionary MLStyleDictionary(this Database db, OpenMode mode = OpenMode.ForRead, bool includingErased = false)
  393.         {
  394.             return db.MLStyleDictionary(db.TransactionManager.TopTransaction, mode, includingErased);
  395.         }
  396.  
  397.         public static LayoutDictionary LayoutDictionary(this Database db, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false)
  398.         {
  399.             if (trx == null)
  400.             {
  401.                 throw new NoActiveTransactionException("No active Transaction");
  402.             }
  403.             DBDictionary dic = (DBDictionary)trx.GetObject(db.LayoutDictionaryId, mode, false, false);
  404.             return includingErased
  405.                 ? new LayoutDictionary(trx, dic, includingErased)
  406.                 : new LayoutDictionary(trx, dic.IncludingErased, includingErased);
  407.         }
  408.  
  409.         public static LayoutDictionary LayoutDictionary(this Database db, OpenMode mode = OpenMode.ForRead, bool includingErased = false)
  410.         {
  411.             return db.LayoutDictionary(db.TransactionManager.TopTransaction, mode, includingErased);
  412.         }
  413.  
  414.         public static int RevisionNumber(this Database db)
  415.         {
  416.             DatabaseSummaryInfo info = db.SummaryInfo;
  417.             string revisionNumberString = info.RevisionNumber;
  418.             int revisionNumber;
  419.             if (!revisionNumberString.IsNullOrEmpty())
  420.             {                
  421.                 if (Int32.TryParse(revisionNumberString, out revisionNumber))
  422.                 {
  423.                     return revisionNumber;
  424.                 }
  425.             }
  426.             revisionNumber = 0;
  427.             DatabaseSummaryInfoBuilder infoBuilder = new DatabaseSummaryInfoBuilder(info);
  428.             infoBuilder.RevisionNumber = revisionNumber.ToString();
  429.             db.SummaryInfo = infoBuilder.ToDatabaseSummaryInfo();
  430.             return revisionNumber;
  431.         }
  432.  
  433.         public static int IncrementRevisionNumber(this Database db)
  434.         {
  435.             return db.AddToRevisionNumber(1);
  436.  
  437.         }
  438.  
  439.         public static int AddToRevisionNumber(this Database db, int number)
  440.         {
  441.             DatabaseSummaryInfo info = db.SummaryInfo;
  442.             string revisionNumberString = info.RevisionNumber;
  443.             int revisionNumber;
  444.             if (revisionNumberString.IsNullOrEmpty() || !Int32.TryParse(revisionNumberString, out revisionNumber))
  445.             {
  446.                 revisionNumber = 0;
  447.             }
  448.  
  449.             int newRevisionNum = revisionNumber + number;
  450.             if (newRevisionNum < 0)
  451.             {
  452.                 newRevisionNum = 0;
  453.             }
  454.             DatabaseSummaryInfoBuilder infoBuilder = new DatabaseSummaryInfoBuilder(db.SummaryInfo);
  455.             infoBuilder.RevisionNumber = newRevisionNum.ToString();
  456.             db.SummaryInfo = infoBuilder.ToDatabaseSummaryInfo();
  457.             return newRevisionNum;
  458.  
  459.         }
  460.     }
  461. }
  462.  




Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #20 on: August 01, 2015, 12:21:08 AM »
Blockreferences
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autodesk.AutoCAD.Runtime;
  6.  
  7. namespace Autodesk.AutoCAD.DatabaseServices
  8. {
  9.     public static class BlockReferenceExtensions
  10.     {
  11.         public static BlockTableRecord GetEffectiveBlockTableRecord(this BlockReference blockref, Transaction trx)
  12.         {
  13.             if (blockref.IsDynamicBlock)
  14.             {
  15.                 return blockref.DynamicBlockTableRecord.GetDBObject<BlockTableRecord>(trx, OpenMode.ForRead, false);
  16.             }
  17.             return blockref.BlockTableRecord.GetDBObject<BlockTableRecord>(trx, OpenMode.ForRead, false);
  18.         }
  19.  
  20.         public static BlockTableRecord GetEffectiveBlockTableRecord(this BlockReference blockref)
  21.         {
  22.             return blockref.GetEffectiveBlockTableRecord(blockref.Database.TransactionManager.TopTransaction);
  23.         }
  24.  
  25.         public static string GetEffectiveName(this BlockReference blockref, Transaction trx)
  26.         {
  27.             if (blockref.IsDynamicBlock)
  28.             {
  29.                 return blockref.DynamicBlockTableRecord.GetDBObject<BlockTableRecord>(trx, OpenMode.ForRead, false).Name;
  30.             }
  31.             return blockref.Name;
  32.         }
  33.  
  34.         public static string GetEffectiveName(this BlockReference blockref)
  35.         {
  36.             return blockref.GetEffectiveName(blockref.Database.TransactionManager.TopTransaction);
  37.            
  38.         }
  39.  
  40.         public static IEnumerable<AttributeReference> GetAttributeReferences(this BlockReference bref, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  41.         {
  42.             if (trx == null)
  43.             {
  44.                 throw new NoActiveTransactionException("No active Transaction");
  45.             }
  46.             if (includingErased)
  47.             {
  48.                 foreach (ObjectId id in bref.AttributeCollection)
  49.                 {
  50.  
  51.                     yield return (AttributeReference)trx.GetObject(id, mode, true, openObjectsOnLockedLayers);
  52.  
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                 foreach (ObjectId id in bref.AttributeCollection)
  58.                 {
  59.                     if (!id.IsErased)
  60.                     {
  61.                         yield return (AttributeReference)trx.GetObject(id, mode, false, openObjectsOnLockedLayers);
  62.                     }
  63.                    
  64.  
  65.                 }
  66.             }
  67.  
  68.  
  69.         }
  70.  
  71.         public static IEnumerable<AttributeReference> GetAttributeReferences(this BlockReference bref, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  72.         {
  73.             return bref.GetAttributeReferences(bref.Database.TransactionManager.TopTransaction, mode, includingErased, openObjectsOnLockedLayers);
  74.  
  75.         }
  76.  
  77.         public static Dictionary<string, AttributeReference> GetAttributeReferenceDictionary(this BlockReference bref, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  78.         {
  79.             if (trx == null)
  80.             {
  81.                 throw new NoActiveTransactionException("No active Transaction");
  82.             }
  83.  
  84.             return bref.GetAttributeReferences(trx, mode, includingErased, openObjectsOnLockedLayers).ToDictionary(a => a.Tag);
  85.  
  86.         }
  87.  
  88.         public static Dictionary<string, AttributeReference> GetAttributeReferenceDictionary(this BlockReference bref, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  89.         {
  90.             return bref.GetAttributeReferenceDictionary(bref.Database.TransactionManager.TopTransaction, mode, includingErased, openObjectsOnLockedLayers);
  91.  
  92.         }
  93.     }
  94. }
  95.  
  96.  

Blocktable
Code - C#: [Select]
  1. using System.Collections.Generic;
  2.  
  3. namespace Autodesk.AutoCAD.DatabaseServices
  4. {
  5.     public static class BlockTableExtensions
  6.     {
  7.         public static IEnumerable<BlockTableRecord> GetBlockTableRecords(this BlockTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  8.         {
  9.             if (filter.IsSet(SymbolTableRecordFilter.IncludeDependent))
  10.             {
  11.                 return symbolTbl.GetSymbolTableRecords<BlockTableRecord>(trx, mode, filter, true);
  12.             }
  13.             return symbolTbl.GetSymbolTableRecords<BlockTableRecord>(trx, mode, filter, true).NonDependent();
  14.         }
  15.  
  16.         public static IEnumerable<BlockTableRecord> GetBlockTableRecords(this BlockTable symbolTbl, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  17.         {
  18.             return symbolTbl.GetBlockTableRecords(symbolTbl.Database.TransactionManager.TopTransaction, mode, filter);
  19.         }
  20.  
  21.  
  22.         public static IEnumerable<BlockTableRecord> GetUserDefinedBlockTableRecords(this BlockTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead)
  23.         {
  24.  
  25.             return symbolTbl.GetSymbolTableRecords<BlockTableRecord>(trx, mode, SymbolTableRecordFilter.None, true).UserDefinedBlocks();
  26.         }
  27.  
  28.         public static IEnumerable<BlockTableRecord> GetUserDefinedBlockTableRecords(this BlockTable symbolTbl, OpenMode mode = OpenMode.ForRead)
  29.         {
  30.             return symbolTbl.GetUserDefinedBlockTableRecords(symbolTbl.Database.TransactionManager.TopTransaction, mode);
  31.         }
  32.  
  33.  
  34.         public static IEnumerable<BlockTableRecord> GetUserCreatedBlockTableRecords(this BlockTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead)
  35.         {
  36.  
  37.             return symbolTbl.GetSymbolTableRecords<BlockTableRecord>(trx, mode, SymbolTableRecordFilter.None, true).UserCreatedBlocks();
  38.         }
  39.  
  40.         public static IEnumerable<BlockTableRecord> GetUserCreatedBlockTableRecords(this BlockTable symbolTbl, OpenMode mode = OpenMode.ForRead)
  41.         {
  42.             return symbolTbl.GetUserCreatedBlockTableRecords(symbolTbl.Database.TransactionManager.TopTransaction, mode);
  43.         }
  44.  
  45.  
  46.  
  47.         internal static ObjectId GetDatabaseBlockTableId(this BlockTable symbolTbl, Database db)
  48.         {
  49.             return db.BlockTableId;
  50.         }
  51.     }
  52.  
  53. }
  54.  
BlockTableRecord
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.AutoCAD.DatabaseServices.Contracts;
  7. using System.Diagnostics.Contracts;
  8. //using Autodesk.AutoCAD.ApplicationServices.Core;
  9. //using Autodesk.AutoCAD.EditorInput;
  10. namespace Autodesk.AutoCAD.DatabaseServices
  11. {
  12.  public static class BlockTableRecordExtensions
  13.     {
  14.  
  15.         /// <summary>
  16.         /// Author: Tony Tanzillo
  17.         /// Source: http://www.theswamp.org/index.php?topic=41311.msg464529#msg464529
  18.         /// </summary>
  19.         /// <param name="source"></param>
  20.         /// <returns></returns>
  21.      public static IEnumerable<BlockTableRecord> UserDefinedBlocks(this IEnumerable<BlockTableRecord> source)
  22.      {
  23.          return source.Where(btr =>
  24.              !(
  25.              btr.IsAnonymous ||
  26.              btr.IsFromExternalReference ||
  27.              btr.IsFromOverlayReference ||
  28.              btr.IsLayout ||
  29.              btr.IsAProxy
  30.              )
  31.              );
  32.      }
  33.  
  34.      public static IEnumerable<BlockTableRecord> UserCreatedBlocks(this IEnumerable<BlockTableRecord> source)
  35.      {
  36.          return source.Where(btr =>
  37.              !(
  38.              btr.IsFromExternalReference ||
  39.              btr.IsFromOverlayReference ||
  40.              btr.IsLayout ||
  41.              btr.IsAProxy
  42.              )
  43.              );
  44.      }
  45.         /// <summary>
  46.         /// Author: Tony Tanzillo
  47.         /// Source: http://www.theswamp.org/index.php?topic=41311.msg464529#msg464529
  48.         /// Use instead of UserDefinedBlocks when anonymous blocks are needed
  49.         /// </summary>
  50.         /// <param name="source"></param>
  51.         /// <returns></returns>
  52.      public static IEnumerable<BlockTableRecord> NonDependent(this IEnumerable<BlockTableRecord> source)
  53.      {
  54.          return source.Where(btr =>
  55.              !(
  56.              btr.IsFromExternalReference ||
  57.              btr.IsFromOverlayReference
  58.              )
  59.              );
  60.      }
  61.  
  62.  
  63.      public static IEnumerable<ObjectId> GetObjectIds<T>(this BlockTableRecord btr) where T : Entity
  64.      {
  65.          IntPtr impobj = RXClass.GetClass(typeof(T)).UnmanagedObject;
  66.          foreach (ObjectId id in btr)
  67.          {
  68.              if (id.ObjectClass.UnmanagedObject == impobj)
  69.              {
  70.                  yield return id;
  71.              }
  72.          }
  73.      }
  74.  
  75.      public static IEnumerable<ObjectId> GetObjectIds(this BlockTableRecord btr)
  76.      {
  77.          foreach (ObjectId id in btr)
  78.          {
  79.              yield return id;
  80.          }
  81.      }
  82.  
  83.         /// <summary>
  84.         /// Author: Tony Tanzillo
  85.         /// A combination of code written by Tony Tanzillo from 2 sources below
  86.         /// Source1: http://www.theswamp.org/index.php?topic=42197.msg474011#msg474011
  87.         /// Source2: http://www.theswamp.org/index.php?topic=41311.msg464457#msg464457
  88.         /// </summary>
  89.         /// <param name="btr"></param>
  90.         /// <param name="mode"></param>
  91.         /// <param name="openErased"></param>
  92.         /// <param name="openObjectsOnLockedLayers"></param>
  93.         /// <returns></returns>
  94.      public static IEnumerable<Entity> GetEntities(this BlockTableRecord btr, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  95.         {
  96.             if (trx == null)
  97.             {
  98.                 throw new NoActiveTransactionException("No active Transaction");
  99.             }
  100.  
  101.             foreach (ObjectId id in includingErased ? btr.IncludingErased : btr)
  102.             {
  103.                 yield return (Entity)trx.GetObject(id, mode, includingErased, openObjectsOnLockedLayers);
  104.             }
  105.  
  106.         }
  107.  
  108.         public static IEnumerable<Entity> GetEntities(this BlockTableRecord btr, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  109.         {
  110.             return btr.GetEntities(btr.Database.TransactionManager.TopTransaction, mode, includingErased, openObjectsOnLockedLayers);
  111.  
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Author: Tony Tanzillo
  116.         /// A combination of code written by Tony Tanzillo from 2 sources below
  117.         /// Source1: http://www.theswamp.org/index.php?topic=42197.msg474011#msg474011
  118.         /// Source2: http://www.theswamp.org/index.php?topic=41311.msg464457#msg464457
  119.         /// </summary>
  120.         /// <typeparam name="T"></typeparam>
  121.         /// <param name="btr"></param>
  122.         /// <param name="mode"></param>
  123.         /// <param name="openErased"></param>
  124.         /// <param name="openObjectsOnLockedLayers"></param>
  125.         /// <returns></returns>
  126.         public static IEnumerable<T> GetEntities<T>(this BlockTableRecord btr, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false) where T : Entity
  127.         {
  128.             if (trx == null)
  129.             {
  130.                 throw new NoActiveTransactionException("No active Transaction");
  131.             }
  132.  
  133.             IntPtr impObject = RXClass.GetClass(typeof(T)).UnmanagedObject;
  134.  
  135.  
  136.             foreach (ObjectId id in includingErased ? btr.IncludingErased : btr)
  137.             {
  138.                 if(id.ObjectClass.UnmanagedObject == impObject)
  139.                 {
  140.                     yield return (T)trx.GetObject(id, mode, includingErased, openObjectsOnLockedLayers);
  141.                 }
  142.  
  143.             }
  144.         }
  145.  
  146.         public static IEnumerable<T> GetEntities<T>(this BlockTableRecord btr, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false) where T : Entity
  147.         {
  148.             return btr.GetEntities<T>(btr.Database.TransactionManager.TopTransaction, mode, includingErased, openObjectsOnLockedLayers);
  149.         }
  150.  
  151.         /// <summary>
  152.         /// Author: Tony Tanzillo
  153.         /// A combination of code written by Tony Tanzillo from 2 sources below
  154.         /// Source1: http://www.theswamp.org/index.php?topic=42197.msg474011#msg474011
  155.         /// Source2: http://www.theswamp.org/index.php?topic=41311.msg464457#msg464457
  156.         /// </summary>
  157.         /// <typeparam name="T"></typeparam>
  158.         /// <param name="btr"></param>
  159.         /// <param name="mode"></param>
  160.         /// <param name="openErased"></param>
  161.         /// <param name="openObjectsOnLockedLayers"></param>
  162.         /// <returns></returns>
  163.         public static IEnumerable<T> GetEntitiesAssignableFrom<T>(this BlockTableRecord btr, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false) where T : Entity
  164.         {
  165.             if (trx == null)
  166.             {
  167.                 throw new NoActiveTransactionException("No active Transaction");
  168.             }
  169.  
  170.             RXClass rxclass = RXClass.GetClass(typeof(T));
  171.  
  172.  
  173.             foreach (ObjectId id in includingErased ? btr.IncludingErased : btr)
  174.             {
  175.                 if (id.ObjectClass.IsDerivedFrom(rxclass))
  176.                 {
  177.                     yield return (T)trx.GetObject(id, mode, includingErased, openObjectsOnLockedLayers);
  178.                 }
  179.  
  180.             }
  181.  
  182.         }
  183.  
  184.         public static IEnumerable<T> GetEntitiesAssignableFrom<T>(this BlockTableRecord btr, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false) where T : Entity
  185.         {
  186.             return btr.GetEntitiesAssignableFrom<T>(btr.Database.TransactionManager.TopTransaction, mode, includingErased, openObjectsOnLockedLayers);
  187.  
  188.         }
  189.      /// <summary>
  190.      /// Method for getting all blockreference Ids
  191.      /// </summary>
  192.      /// <param name="btr"></param>
  193.      /// <param name="directOnly"></param>
  194.      /// <param name="forceValidity"></param>
  195.      /// <param name="trx"></param>
  196.      /// <returns></returns>
  197.         public static ObjectIdCollection GetAllBlockReferenceIds(this BlockTableRecord btr, Transaction trx, bool directOnly, bool forceValidity)
  198.         {
  199.             if (trx == null)
  200.             {
  201.                 throw new NoActiveTransactionException("No active Transaction");
  202.             }
  203.  
  204.             ObjectIdCollection blockReferenceIds = btr.GetBlockReferenceIds(directOnly, forceValidity);
  205.  
  206.          if (!btr.IsDynamicBlock) return blockReferenceIds;
  207.          foreach (ObjectId id in btr.GetAnonymousBlockIds())
  208.          {
  209.              BlockTableRecord record = trx.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
  210.              blockReferenceIds.Add(record.GetBlockReferenceIds(directOnly, forceValidity));
  211.  
  212.          }
  213.          return blockReferenceIds;
  214.         }
  215.  
  216.         public static ObjectIdCollection GetAllBlockReferenceIds(this BlockTableRecord btr, bool directOnly, bool forceValidity)
  217.         {
  218.             return btr.GetAllBlockReferenceIds(btr.Database.TransactionManager.TopTransaction, directOnly, forceValidity);
  219.         }
  220.  
  221.         public static IEnumerable<BlockReference> GetAllBlockReferences(this BlockTableRecord btr, Transaction trx, bool directOnly, bool forceValidity)
  222.         {
  223.             TransactionManager tm = btr.Database.TransactionManager;
  224.             foreach (ObjectId id in GetAllBlockReferenceIds(btr, trx, directOnly, forceValidity))
  225.             {
  226.  
  227.                 yield return (BlockReference)trx.GetObject(id, OpenMode.ForRead, false, false);
  228.             }
  229.            
  230.         }
  231.  
  232.         public static IEnumerable<BlockReference> GetAllBlockReferences(this BlockTableRecord btr, bool directOnly, bool forceValidity)
  233.         {
  234.             return btr.GetAllBlockReferences(btr.Database.TransactionManager.TopTransaction, directOnly, forceValidity);
  235.         }
  236.  
  237.  
  238.  
  239.      public static IEnumerable<AttributeDefinition> GetAttributeDefinitions(this BlockTableRecord btr, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  240.      {
  241.  
  242.          return btr.GetEntities<AttributeDefinition>(trx, mode, includingErased, openObjectsOnLockedLayers);
  243.      }
  244.  
  245.      public static IEnumerable<AttributeDefinition> GetAttributeDefinitions(this BlockTableRecord btr, OpenMode mode = OpenMode.ForRead, bool includingErased = false, bool openObjectsOnLockedLayers = false)
  246.      {
  247.          return btr.GetAttributeDefinitions(btr.Database.TransactionManager.TopTransaction, mode, includingErased, openObjectsOnLockedLayers);
  248.      }
  249.     }
  250. }
  251.  
  252.  
  253.  


Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #21 on: August 01, 2015, 12:34:44 AM »
Objectid
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autodesk.AutoCAD.DatabaseServices.Contracts;
  6. using System.Diagnostics.Contracts;
  7. using Autodesk.AutoCAD.Runtime;
  8.  
  9. namespace Autodesk.AutoCAD.DatabaseServices
  10. {
  11.     public static class ObjectIdExtensions
  12.     {
  13.  
  14.         public static T GetDBObject<T>(this ObjectId id, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false) where T : DBObject
  15.         {
  16.             if (id.IsNull)
  17.             {
  18.                 throw new ArgumentNullException("id is null");
  19.             }
  20.             if (trx == null)
  21.             {
  22.                 throw new NoActiveTransactionException("No active Transaction");
  23.             }
  24.             return (T)trx.GetObject(id, mode, openErased, false);
  25.         }
  26.  
  27.         public static T GetDBObject<T>(this ObjectId id, OpenMode mode = OpenMode.ForRead, bool openErased = false) where T : DBObject
  28.         {
  29.             return id.GetDBObject<T>(id.Database.TransactionManager.TopTransaction, mode, openErased);
  30.         }
  31.  
  32.         public static DBObject GetDBObject(this ObjectId id, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false)
  33.         {
  34.             return id.GetDBObject<DBObject>(trx, mode, openErased);
  35.         }
  36.  
  37.         public static DBObject GetDBObject(this ObjectId id, OpenMode mode = OpenMode.ForRead, bool openErased = false)
  38.         {
  39.             return id.GetDBObject<DBObject>(id.Database.TransactionManager.TopTransaction, mode, openErased);
  40.         }
  41.  
  42.  
  43.  
  44.  
  45.         public static T GetEntity<T>(this ObjectId id, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false) where T : Entity
  46.         {
  47.             if (id.IsNull)
  48.             {
  49.                 throw new ArgumentNullException("id is null");
  50.             }
  51.             if (trx == null)
  52.             {
  53.                 throw new NoActiveTransactionException("No active Transaction");
  54.             }
  55.             return (T)trx.GetObject(id, mode, openErased, forceOpenOnLockedLayer);
  56.         }
  57.        
  58.  
  59.         public static T GetEntity<T>(this ObjectId id, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false) where T : Entity
  60.         {
  61.             return id.GetEntity<T>(id.Database.TransactionManager.TopTransaction, mode, openErased, forceOpenOnLockedLayer);
  62.         }
  63.  
  64.  
  65.         public static Entity GetEntity(this ObjectId id, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false)
  66.         {
  67.             return id.GetEntity<Entity>(trx, mode, openErased, forceOpenOnLockedLayer);
  68.         }
  69.  
  70.         public static Entity GetEntity(this ObjectId id, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false)
  71.         {
  72.             return id.GetEntity<Entity>(id.Database.TransactionManager.TopTransaction, mode, openErased, forceOpenOnLockedLayer);
  73.         }
  74.  
  75.         public static IEnumerable<T> GetEntities<T>(this IEnumerable<ObjectId> ids, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false) where T : Entity
  76.         {
  77.             if (ids == null)
  78.             {
  79.                 throw new ArgumentNullException("ids is null");
  80.             }
  81.             if (trx == null)
  82.             {
  83.                 throw new NoActiveTransactionException("No active Transaction");
  84.             }
  85.             foreach (ObjectId id in ids)
  86.             {
  87.                yield return (T)trx.GetObject(id, mode, openErased, forceOpenOnLockedLayer);
  88.             }
  89.            
  90.         }
  91.  
  92.         public static IEnumerable<T> GetEntities<T>(this IEnumerable<ObjectId> ids, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false) where T : Entity
  93.         {
  94.             ObjectId id = ids.First();
  95.             if (id == null)
  96.             {
  97.                 throw new ArgumentNullException("id is null");
  98.             }
  99.             return ids.GetEntities<T>(id.Database.TransactionManager.TopTransaction, mode, openErased, forceOpenOnLockedLayer);
  100.         }
  101.  
  102.         public static IEnumerable<Entity> GetEntities(this IEnumerable<ObjectId> ids, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false)
  103.         {
  104.              return ids.GetEntities<Entity>(trx, mode, openErased, forceOpenOnLockedLayer);
  105.  
  106.         }
  107.  
  108.         public static IEnumerable<Entity> GetEntities(this IEnumerable<ObjectId> ids, OpenMode mode = OpenMode.ForRead, bool openErased = false, bool forceOpenOnLockedLayer = false)
  109.         {
  110.             ObjectId id = ids.First();
  111.             if (id == null)
  112.             {
  113.                 throw new ArgumentNullException("id is null");
  114.             }
  115.             return ids.GetEntities<Entity>(id.Database.TransactionManager.TopTransaction, mode, openErased, forceOpenOnLockedLayer);
  116.         }
  117.  
  118.         public static IEnumerable<T> GetDBObjects<T>(this IEnumerable<ObjectId> ids, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false) where T : DBObject
  119.         {
  120.             if (ids == null)
  121.             {
  122.                 throw new ArgumentNullException("ids is null");
  123.             }
  124.             if (trx == null)
  125.             {
  126.                 throw new NoActiveTransactionException("No active Transaction");
  127.             }
  128.             foreach (ObjectId id in ids)
  129.             {
  130.                 yield return (T)trx.GetObject(id, mode, openErased, false);
  131.             }
  132.  
  133.         }
  134.  
  135.         public static IEnumerable<T> GetDBObjects<T>(this IEnumerable<ObjectId> ids, OpenMode mode = OpenMode.ForRead, bool openErased = false) where T : DBObject
  136.         {
  137.             ObjectId id = ids.First();
  138.             if (id == null)
  139.             {
  140.                 throw new ArgumentNullException("id is null");
  141.             }
  142.             return ids.GetDBObjects<T>(id.Database.TransactionManager.TopTransaction, mode, openErased);
  143.         }
  144.  
  145.         public static IEnumerable<DBObject> GetDBObjects(this IEnumerable<ObjectId> ids, Transaction trx, OpenMode mode = OpenMode.ForRead, bool openErased = false)
  146.         {
  147.             return ids.GetDBObjects<DBObject>(trx, mode, openErased);
  148.  
  149.         }
  150.  
  151.         public static IEnumerable<DBObject> GetDBObjects(this IEnumerable<ObjectId> ids, OpenMode mode = OpenMode.ForRead, bool openErased = false)
  152.         {
  153.             ObjectId id = ids.First();
  154.             if (id == null)
  155.             {
  156.                 throw new ArgumentNullException("id is null");
  157.             }
  158.             return ids.GetDBObjects<DBObject>(id.Database.TransactionManager.TopTransaction, mode, openErased);
  159.         }
  160.     }
  161. }
  162.  
  163.  

SymbolTable
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autodesk.AutoCAD.DatabaseServices.Contracts;
  6. using Autodesk.AutoCAD.Runtime;
  7.  
  8. namespace Autodesk.AutoCAD.DatabaseServices
  9. {
  10.     public static class SymbolTableExtensions
  11.     {
  12.         public static IEnumerable<ObjectId> GetObjectIds(this SymbolTable st)
  13.         {
  14.             foreach (ObjectId id in st)
  15.             {
  16.                 yield return id;
  17.             }
  18.         }
  19.  
  20.         internal static IEnumerable<T> GetSymbolTableRecords<T>(this SymbolTable symbolTbl, Transaction trx,
  21.             OpenMode mode, SymbolTableRecordFilter filter, bool filterDependecyById) where T : SymbolTableRecord
  22.         {
  23.             if (trx == null)
  24.             {
  25.                 throw new NoActiveTransactionException("No active Transaction");
  26.             }
  27.  
  28.             bool includingErased = filter.IsSet(SymbolTableRecordFilter.IncludedErased);
  29.  
  30.             if (filter.IsSet(SymbolTableRecordFilter.IncludeDependent))
  31.             {
  32.                 foreach (ObjectId id in includingErased ? symbolTbl.IncludingErased : symbolTbl)
  33.                 {
  34.                     yield return (T)trx.GetObject(id, mode, includingErased, false);
  35.                 }
  36.             }
  37.  
  38.             else
  39.             {
  40.                 if (filterDependecyById)
  41.                 {
  42.                     IntPtr dbIntPtr = symbolTbl.Database.UnmanagedObject;
  43.                     foreach (ObjectId id in includingErased ? symbolTbl.IncludingErased : symbolTbl)
  44.                     {
  45.                         if (id.OriginalDatabase.UnmanagedObject == dbIntPtr)
  46.                         {
  47.                             yield return (T)trx.GetObject(id, mode, includingErased, false);
  48.                         }
  49.                     }
  50.                 }
  51.                 else
  52.                 {
  53.                     foreach (ObjectId id in includingErased ? symbolTbl.IncludingErased : symbolTbl)
  54.                     {
  55.                         T current = (T)trx.GetObject(id, mode, includingErased, false);
  56.                         if (!current.IsDependent)
  57.                         {
  58.                             yield return current;
  59.                         }
  60.                     }
  61.                 }
  62.  
  63.             }
  64.  
  65.  
  66.         }
  67.  
  68.  
  69.         /// <summary>
  70.         /// Author: Gile
  71.         /// Source: http://www.theswamp.org/index.php?topic=42539.msg477455#msg477455
  72.         /// </summary>
  73.         /// <param name="source"></param>
  74.         /// <returns></returns>
  75.         public static ObjectId ImportSymbolTableRecord<T>(this Database targetDb, string sourceFile,
  76.             DuplicateRecordCloning cloningStyle, string recordName)
  77.             where T : SymbolTable
  78.         {
  79.  
  80.             using (var sourceDb = new Database(false, true))
  81.             {
  82.                 sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.ReadWrite, false, "");
  83.                 ObjectId sourceTableId, targetTableId;
  84.                 switch (typeof (T).Name)
  85.                 {
  86.                     case "BlockTable":
  87.                         sourceTableId = sourceDb.BlockTableId;
  88.                         targetTableId = targetDb.BlockTableId;
  89.                         break;
  90.                     case "DimStyleTable":
  91.                         sourceTableId = sourceDb.DimStyleTableId;
  92.                         targetTableId = targetDb.DimStyleTableId;
  93.                         break;
  94.                     case "LayerTable":
  95.                         sourceTableId = sourceDb.LayerTableId;
  96.                         targetTableId = targetDb.LayerTableId;
  97.                         break;
  98.                     case "LinetypeTable":
  99.                         sourceTableId = sourceDb.LinetypeTableId;
  100.                         targetTableId = targetDb.LinetypeTableId;
  101.                         break;
  102.                     case "RegAppTable":
  103.                         sourceTableId = sourceDb.RegAppTableId;
  104.                         targetTableId = targetDb.RegAppTableId;
  105.                         break;
  106.                     case "TextStyleTable":
  107.                         sourceTableId = sourceDb.TextStyleTableId;
  108.                         targetTableId = targetDb.TextStyleTableId;
  109.                         break;
  110.                     case "UcsTable":
  111.                         sourceTableId = sourceDb.UcsTableId;
  112.                         targetTableId = targetDb.UcsTableId;
  113.                         break;
  114.                     case "ViewTable":
  115.                         sourceTableId = sourceDb.ViewportTableId;
  116.                         targetTableId = targetDb.ViewportTableId;
  117.                         break;
  118.                     case "ViewportTable":
  119.                         sourceTableId = sourceDb.ViewportTableId;
  120.                         targetTableId = targetDb.ViewportTableId;
  121.                         break;
  122.                     default:
  123.                         throw new ArgumentException("Requires a concrete type derived from SymbolTable");
  124.                 }
  125.  
  126.                 using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
  127.                 {
  128.  
  129.                     T sourceTable = (T) tr.GetObject(sourceTableId, OpenMode.ForRead);
  130.                     if (!sourceTable.Has(recordName))
  131.                     {
  132.                         return ObjectId.Null;
  133.                     }
  134.                     var idCol = new ObjectIdCollection();
  135.                     ObjectId sourceTableRecordId = sourceTable[recordName];
  136.                     idCol.Add(sourceTableRecordId);
  137.                     var idMap = new IdMapping();
  138.                     sourceDb.WblockCloneObjects(idCol, targetTableId, idMap, cloningStyle, false);
  139.                     tr.Commit();
  140.                     return idMap[sourceTableRecordId].Value;
  141.                 }
  142.             }
  143.         }
  144.  
  145.  
  146.         /// <summary>
  147.         /// Author: Gile
  148.         /// Source: http://www.theswamp.org/index.php?topic=42539.msg477455#msg477455
  149.         /// </summary>
  150.         /// <param name="source"></param>
  151.         /// <returns></returns>
  152.         public static bool ImportSymbolTableRecords<T>(this Database targetDb, string sourceFile,
  153.             DuplicateRecordCloning cloningStyle, params string[] recordNames)
  154.             where T : SymbolTable
  155.         {
  156.             using (var sourceDb = new Database(false, true))
  157.             {
  158.                 sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, false, "");
  159.                 ObjectId sourceTableId, targetTableId;
  160.                 switch (typeof (T).Name)
  161.                 {
  162.                     case "BlockTable":
  163.                         sourceTableId = sourceDb.BlockTableId;
  164.                         targetTableId = targetDb.BlockTableId;
  165.                         break;
  166.                     case "DimStyleTable":
  167.                         sourceTableId = sourceDb.DimStyleTableId;
  168.                         targetTableId = targetDb.DimStyleTableId;
  169.                         break;
  170.                     case "LayerTable":
  171.                         sourceTableId = sourceDb.LayerTableId;
  172.                         targetTableId = targetDb.LayerTableId;
  173.                         break;
  174.                     case "LinetypeTable":
  175.                         sourceTableId = sourceDb.LinetypeTableId;
  176.                         targetTableId = targetDb.LinetypeTableId;
  177.                         break;
  178.                     case "RegAppTable":
  179.                         sourceTableId = sourceDb.RegAppTableId;
  180.                         targetTableId = targetDb.RegAppTableId;
  181.                         break;
  182.                     case "TextStyleTable":
  183.                         sourceTableId = sourceDb.TextStyleTableId;
  184.                         targetTableId = targetDb.TextStyleTableId;
  185.                         break;
  186.                     case "UcsTable":
  187.                         sourceTableId = sourceDb.UcsTableId;
  188.                         targetTableId = targetDb.UcsTableId;
  189.                         break;
  190.                     case "ViewTable":
  191.                         sourceTableId = sourceDb.ViewportTableId;
  192.                         targetTableId = targetDb.ViewportTableId;
  193.                         break;
  194.                     case "ViewportTable":
  195.                         sourceTableId = sourceDb.ViewportTableId;
  196.                         targetTableId = targetDb.ViewportTableId;
  197.                         break;
  198.                     default:
  199.                         throw new ArgumentException("Requires a concrete type derived from SymbolTable");
  200.                 }
  201.  
  202.                 using (var tr = sourceDb.TransactionManager.StartTransaction())
  203.                 {
  204.                     var allImported = true;
  205.                     var sourceTable = (T) tr.GetObject(sourceTableId, OpenMode.ForRead);
  206.                     var idCol = new ObjectIdCollection();
  207.                     foreach (var recordName in recordNames)
  208.                     {
  209.                         if (sourceTable.Has(recordName))
  210.                         {
  211.                             idCol.Add(sourceTable[recordName]);
  212.                         }
  213.                         else
  214.                         {
  215.                             allImported = false;
  216.                         }
  217.                     }
  218.  
  219.                     var idMap = new IdMapping();
  220.                     sourceDb.WblockCloneObjects(idCol, targetTableId, idMap, cloningStyle, false);
  221.                     tr.Commit();
  222.                     return allImported;
  223.                 }
  224.             }
  225.         }
  226.  
  227.     }
  228. }
  229.  

DBDictionary
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autodesk.AutoCAD.Runtime;
  6. using System.Diagnostics.Contracts;
  7. using Autodesk.AutoCAD.DatabaseServices.Contracts;
  8.  
  9. namespace Autodesk.AutoCAD.DatabaseServices
  10. {
  11.     public static class DBDictionaryExtensions
  12.     {
  13.         public static IEnumerable<T> GetEntries<T>(this DBDictionary dic, Transaction trx, OpenMode mode = OpenMode.ForRead, bool includingErased = false) where T : DBObject
  14.         {
  15.             if (trx == null)
  16.             {
  17.                 throw new NoActiveTransactionException("No active Transaction");
  18.             }
  19.             foreach (var entry in includingErased ? dic.IncludingErased : dic)
  20.             {
  21.                 yield return (T)trx.GetObject(entry.Value, mode, includingErased, false);
  22.             }
  23.         }
  24.  
  25.         public static IEnumerable<T> GetEntries<T>(this DBDictionary dic, OpenMode mode = OpenMode.ForRead, bool includingErased = false) where T : DBObject
  26.         {
  27.             return dic.GetEntries<T>(dic.Database.TransactionManager.TopTransaction, mode, includingErased);
  28.         }
  29.     }
  30. }
  31.  
  32.  
DimstyleTable
Code - C#: [Select]
  1. using System.Collections.Generic;
  2.  
  3. namespace Autodesk.AutoCAD.DatabaseServices
  4. {
  5.     public static class DimStyleTableExtensions
  6.     {
  7.  
  8.         public static IEnumerable<DimStyleTableRecord> GetDimStyleTableRecords(this DimStyleTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  9.         {
  10.             return symbolTbl.GetSymbolTableRecords<DimStyleTableRecord>(trx, mode, filter, true);
  11.         }
  12.  
  13.         public static IEnumerable<DimStyleTableRecord> GetDimStyleTableRecords(this DimStyleTable symbolTbl, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  14.         {
  15.             return symbolTbl.GetSymbolTableRecords<DimStyleTableRecord>(symbolTbl.Database.TransactionManager.TopTransaction, mode, filter, true);
  16.         }
  17.  
  18.     }
  19. }
  20.  
  21.  
LayerTable
Code - C#: [Select]
  1. using System.Collections.Generic;
  2.  
  3. namespace Autodesk.AutoCAD.DatabaseServices
  4. {
  5.     public static class LayerTableExtensions
  6.     {
  7.  
  8.         public static IEnumerable<LayerTableRecord> GetLayerTableRecords(this LayerTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  9.         {
  10.             return symbolTbl.GetSymbolTableRecords<LayerTableRecord>(trx, mode, filter, false);
  11.         }
  12.  
  13.         public static IEnumerable<LayerTableRecord> GetLayerTableRecords(this LayerTable symbolTbl, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  14.         {
  15.             return symbolTbl.GetSymbolTableRecords<LayerTableRecord>(symbolTbl.Database.TransactionManager.TopTransaction, mode, filter, false);
  16.         }
  17.  
  18.     }
  19.  
  20.  
  21. }
  22.  
  23.  


Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #22 on: August 01, 2015, 12:42:12 AM »
LineTypeTable
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Autodesk.AutoCAD.DatabaseServices
  7. {
  8.  
  9.  
  10.     public static class LinetypeTableExtensions
  11.     {
  12.  
  13.         public static IEnumerable<LinetypeTableRecord> GetLinetypeTableRecords(this LinetypeTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  14.         {
  15.             return symbolTbl.GetSymbolTableRecords<LinetypeTableRecord>(trx, mode, filter, false);
  16.         }
  17.  
  18.         public static IEnumerable<LinetypeTableRecord> GetLinetypeTableRecords(this LinetypeTable symbolTbl, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  19.         {
  20.             return symbolTbl.GetSymbolTableRecords<LinetypeTableRecord>(symbolTbl.Database.TransactionManager.TopTransaction, mode, filter, false);
  21.         }
  22.  
  23.     }
  24. }
  25.  
  26.  

ObjectIDCollection
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Autodesk.AutoCAD.DatabaseServices
  7. {
  8.     public static class ObjectIdCollectionExtensions
  9.     {
  10.        
  11.         public static void Add(this ObjectIdCollection thisIds, ObjectIdCollection ids)
  12.         {
  13.             foreach (ObjectId id in ids)
  14.             {
  15.                 thisIds.Add(id);
  16.             }
  17.         }
  18.  
  19.         public static void Add(this ObjectIdCollection thisIds, IEnumerable<ObjectId> ids)
  20.         {
  21.             foreach (ObjectId id in ids)
  22.             {
  23.                 thisIds.Add(id);
  24.             }
  25.         }
  26.  
  27.  
  28.  
  29.         public static ObjectId[] ToArray(this ObjectIdCollection ids)
  30.         {
  31.             ObjectId[] idsArray = new ObjectId[ids.Count];
  32.             ids.CopyTo(idsArray, 0);
  33.             return idsArray;
  34.         }
  35.  
  36.         //public static ObjectId[] ToArray(this IEnumerable<ObjectId> ids)
  37.         //{
  38.         //    return ids.ToArray();
  39.         //}
  40.  
  41.         public static IEnumerable<ObjectId> Where<T>(this ObjectIdCollection source, Transaction trx, Func<T, bool> predicate) where T : DBObject
  42.         {
  43.             if (source.IsNull())
  44.             {
  45.                 throw new ArgumentNullException("source");
  46.             }
  47.             if (predicate.IsNull())
  48.             {
  49.                 throw new ArgumentNullException("predicate");
  50.             }
  51.             return WhereImpl<T>(source, trx, predicate);
  52.         }
  53.  
  54.         public static IEnumerable<ObjectId> Where<T>(this ObjectIdCollection source, Func<T, bool> predicate) where T : DBObject
  55.         {
  56.  
  57.             return Where<T>(source, HostApplicationServices.WorkingDatabase.TransactionManager.TopTransaction,  predicate);
  58.         }
  59.  
  60.         private static IEnumerable<ObjectId> WhereImpl<T>(this ObjectIdCollection source, Transaction trx, Func<T, bool> predicate) where T : DBObject
  61.         {
  62.            
  63.             foreach (ObjectId item in source)
  64.             {
  65.                 T dbo = (T)trx.GetObject(item, OpenMode.ForRead, false, false);
  66.                 if (predicate(dbo))
  67.                 {
  68.                     yield return item;
  69.                 }
  70.  
  71.             }
  72.         }
  73.         public static ObjectIdCollection ToObjectIdCollection(this IEnumerable<ObjectId> source)
  74.         {
  75.  
  76.             return new ObjectIdCollection(source.ToArray());
  77.         }
  78.  
  79.         public static ObjectIdCollection ToObjectIdCollection(this IEnumerable<DBObject> source)
  80.         {
  81.             ObjectIdCollection ids = new ObjectIdCollection();
  82.             foreach (var dbObject in source)
  83.             {
  84.                 ids.Add(dbObject.ObjectId);
  85.             }
  86.             return ids;
  87.         }
  88.  
  89.  
  90.     }
  91. }
  92.  

TextStyleTableRecords
Code - C#: [Select]
  1. using System.Collections.Generic;
  2. using System.Linq;
  3.  
  4. namespace Autodesk.AutoCAD.DatabaseServices
  5. {
  6.     public static class TextStyleTableExtensions
  7.     {
  8.  
  9.  
  10.         public static IEnumerable<TextStyleTableRecord> GetAllTextStyleTableRecords(this TextStyleTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  11.         {
  12.             return symbolTbl.GetSymbolTableRecords<TextStyleTableRecord>(trx, mode, filter, true);
  13.         }
  14.  
  15.         public static IEnumerable<TextStyleTableRecord> GetAllTextStyleTableRecords(this TextStyleTable symbolTbl, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  16.         {
  17.             return symbolTbl.GetSymbolTableRecords<TextStyleTableRecord>(symbolTbl.Database.TransactionManager.TopTransaction, mode, filter, true);
  18.         }
  19.  
  20.         public static IEnumerable<TextStyleTableRecord> GetTextStyleTableRecords(this TextStyleTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  21.         {
  22.             return symbolTbl.GetSymbolTableRecords<TextStyleTableRecord>(trx, mode, filter, true).Where(txt => !txt.IsShapeFile);
  23.         }
  24.  
  25.         public static IEnumerable<TextStyleTableRecord> GetTextStyleTableRecords(this TextStyleTable symbolTbl, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  26.         {
  27.             return symbolTbl.GetTextStyleTableRecords(symbolTbl.Database.TransactionManager.TopTransaction, mode, filter);
  28.         }
  29.  
  30.         public static IEnumerable<TextStyleTableRecord> GetShapeFileTableRecords(this TextStyleTable symbolTbl, Transaction trx, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  31.         {
  32.             return symbolTbl.GetSymbolTableRecords<TextStyleTableRecord>(trx, mode, filter, true).Where(txt => txt.IsShapeFile);
  33.         }
  34.  
  35.         public static IEnumerable<TextStyleTableRecord> GetShapeFileTableRecords(this TextStyleTable symbolTbl, OpenMode mode = OpenMode.ForRead, SymbolTableRecordFilter filter = SymbolTableRecordFilter.None)
  36.         {
  37.             return symbolTbl.GetShapeFileTableRecords(symbolTbl.Database.TransactionManager.TopTransaction, mode, filter);
  38.         }
  39.  
  40.     }
  41. }
  42.  

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #23 on: August 01, 2015, 12:46:26 AM »
Not sure how other than post whole project

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Screw it I give up
« Reply #24 on: August 01, 2015, 02:02:54 PM »
Great stuff, I can't believe I didn't find this thread earlier!

Thanks Jeff, for posting, and to others (Gile + Tony) for their contributions.

Just getting a look at how you organize a project is a huge help for a newb like me.

And the code..  :smitten:

Thanks again.
« Last Edit: August 01, 2015, 09:14:42 PM by Atook »

sybold

  • Newt
  • Posts: 62
Re: Screw it I give up
« Reply #25 on: August 04, 2015, 07:09:19 AM »
would a git repository not be a better solution to share and exchange the code, others can submit code easily that way too

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Screw it I give up
« Reply #26 on: August 07, 2015, 12:10:02 AM »
You guys must have an incredible library of pretty cool stuff :) Thanks for sharing.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #27 on: August 14, 2015, 03:19:57 PM »
Looking for a few more folks to join private repo on github to help clean up a little and document some before making public, anyone interested let me know.

sybold

  • Newt
  • Posts: 62
Re: Screw it I give up
« Reply #28 on: August 15, 2015, 08:46:40 AM »
sure, no problem

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Screw it I give up
« Reply #29 on: August 15, 2015, 09:58:17 AM »
PM me or post your github username or email registered with github