Author Topic: eDuplicateRecordName  (Read 5405 times)

0 Members and 1 Guest are viewing this topic.

JONTHEPOPE

  • Guest
eDuplicateRecordName
« on: August 06, 2014, 08:22:27 PM »
When I run my code to create layers twice it gives me this error.

eDuplicateRecordName

If I set my layer table record to write will this fix it?

I wanted the layers in my code to overwrite the existing like a script file would.

Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Autodesk.AutoCAD.Runtime;
  7. using Autodesk.AutoCAD.ApplicationServices;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Autodesk.AutoCAD.Colors;
  10.  
  11. namespace mylayerClassLibrary1
  12. {
  13.     public class Class1
  14.     {
  15.         [CommandMethod("SetLayerColor")]
  16.         public static void SetLayerColor()
  17.         {
  18.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  19.             Database acCurDb = acDoc.Database;
  20.  
  21.             using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  22.             {
  23.  
  24.                 LinetypeTable acLinTbl;
  25.                 acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
  26.                                                 OpenMode.ForRead) as LinetypeTable;
  27.  
  28.                 string[] acLineTyps = new string[3];
  29.                 acLineTyps[0] = "BORDER";
  30.                 acLineTyps[1] = "BORDER2";
  31.                 acLineTyps[2] = "BORDERX2";
  32.  
  33.                 foreach (string acLineTyp in acLineTyps)
  34.                 {
  35.                     if (!acLinTbl.Has("acLineTyps"))
  36.                     {
  37.                         acCurDb.LoadLineTypeFile(acLineTyp, "C:/Program Files/Autodesk/AutoCAD 2014/linetypes/acad");
  38.                     }
  39.                 }
  40.  
  41.                 LayerTable acLyrTbl;
  42.                 acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
  43.                                                 OpenMode.ForRead) as LayerTable;
  44.  
  45.                 string[] sLayerNames = new string[3];
  46.                 sLayerNames[0] = "G-ANNO-REFR";
  47.                 sLayerNames[1] = "M-ANNO-TTLB";
  48.                 sLayerNames[2] = "M-ANNO-TEXT-NEWW";
  49.                
  50.                 Color[] acColors = new Color[3];
  51.                 acColors[0] = Color.FromColorIndex(ColorMethod.ByAci, 15);
  52.                 acColors[1] = Color.FromColorIndex(ColorMethod.ByAci, 7);
  53.                 acColors[2] = Color.FromColorIndex(ColorMethod.ByAci, 7);
  54.                
  55.                 int nCnt = 0;
  56.  
  57.                 foreach (string sLayerName in sLayerNames)
  58.                 {
  59.                     if (!acLyrTbl.Has(sLayerName))
  60.                     {
  61.                         using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
  62.                         {
  63.                             acLyrTblRec.Name = sLayerName;
  64.  
  65.                             if (!acLyrTbl.IsWriteEnabled) acLyrTbl.UpgradeOpen();
  66.  
  67.                             acLyrTbl.Add(acLyrTblRec);
  68.                             acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  69.  
  70.                             acLyrTblRec.Color = acColors[nCnt];
  71.  
  72.                         }
  73.                     }
  74.                     else
  75.                     {
  76.                         LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
  77.                                                                          OpenMode.ForWrite) as LayerTableRecord;
  78.  
  79.                         acLyrTblRec.Color = acColors[nCnt];
  80.                     }
  81.  
  82.                     ++ nCnt;
  83.                 }
  84.  
  85.                 // Open the Layer table for read
  86.  
  87.                 string NEWsLayerName = "G-ANNO-REFR";
  88.  
  89.                 {
  90.                     LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[NEWsLayerName],
  91.                                                    OpenMode.ForRead) as LayerTableRecord;
  92.  
  93.                     if (acLinTbl.Has("BORDERX2") == true)
  94.                     {
  95.                         acLyrTblRec.UpgradeOpen();
  96.                         acLyrTblRec.LinetypeObjectId = acLinTbl["BORDERX2"];
  97.                     }
  98.                 }
  99.  
  100.  
  101.                 string ANEWsLayerName = "M-ANNO-TTLB";
  102.  
  103.                 {
  104.                     LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[ANEWsLayerName],
  105.                                                    OpenMode.ForRead) as LayerTableRecord;
  106.  
  107.                     if (acLinTbl.Has("BORDER2") == true)
  108.                     {
  109.                         acLyrTblRec.UpgradeOpen();
  110.                         acLyrTblRec.LinetypeObjectId = acLinTbl["BORDER2"];
  111.                     }
  112.                 }
  113.  
  114.                 string BNEWsLayerName = "M-ANNO-TEXT-NEWW";
  115.  
  116.                 {
  117.                     LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[BNEWsLayerName],
  118.                                                    OpenMode.ForRead) as LayerTableRecord;
  119.  
  120.                     if (acLinTbl.Has("BORDER") == true)
  121.                     {
  122.                         acLyrTblRec.UpgradeOpen();
  123.                         acLyrTblRec.LinetypeObjectId = acLinTbl["BORDER"];
  124.                     }
  125.                 }
  126.  
  127.                 acTrans.Commit();
  128.             }
  129.         }
  130.     }
  131. }
  132.  
  133.  
« Last Edit: August 07, 2014, 11:33:43 PM by JONTHEPOPE »

Jeff H

  • Needs a day job
  • Posts: 6144
Re: eDuplicateRecordName
« Reply #1 on: August 07, 2014, 09:56:18 AM »

Whenever you create a new object it is already open for write and no need to check if it is write enabled.

Code - C: [Select]
  1.  using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
  2.                         {
  3.                             acLyrTblRec.Name = sLayerName;
  4.  
  5.                             if (!acLyrTbl.IsWriteEnabled) acLyrTbl.UpgradeOpen();
  6.  
  7.                             acLyrTbl.Add(acLyrTblRec);
  8.                             acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  9.  
  10.                             acLyrTblRec.Color = acColors[nCnt];
  11.  
  12.                         }
  13.  
  14.  
  15.  

JONTHEPOPE

  • Guest
Re: eDuplicateRecordName
« Reply #2 on: August 07, 2014, 11:59:49 PM »
this code works for creating layers it is just not as good as a script file I have.

the .net version is much faster than my script but there are 2 problems that make the script file to make layers a better choice.

- an error that happens when I run the code twice. eDuplicateRecordName.

- if I change a layer colour in AutoCAD and rerun the code. the code does not over write the layer colour to the one in the .dll. (bad for standards)

 This will take awhile before I can say .net is better at setting up layers for AutoCAD than a script file.

 :angel: Jesus loves you
« Last Edit: August 08, 2014, 12:43:43 AM by JONTHEPOPE »

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: eDuplicateRecordName
« Reply #3 on: August 08, 2014, 08:57:54 AM »
this code works for creating layers it is just not as good as a script file I have.

the .net version is much faster than my script but there are 2 problems that make the script file to make layers a better choice.

- an error that happens when I run the code twice. eDuplicateRecordName.

- if I change a layer colour in AutoCAD and rerun the code. the code does not over write the layer colour to the one in the .dll. (bad for standards)

 This will take awhile before I can say .net is better at setting up layers for AutoCAD than a script file.

 :angel: Jesus loves you

HUH?
Revit 2019, AMEP 2019 64bit Win 10

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: eDuplicateRecordName
« Reply #4 on: August 08, 2014, 09:43:18 AM »
This works every time, all the time.  Just because someone won't do the the work for you and post the answer, don't assume it cannot be done.  A little better understanding of the .NET language and a little more research into the AutoCAD API would've gotten you the answers you were looking for.  To think that any Autodesk script file could out perform the .NET platform is ignorant.

Code - C#: [Select]
  1. using System.Collections.Generic;
  2. using Autodesk.AutoCAD.Colors;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  6.  
  7. namespace LayerLibrary
  8. {
  9.     public class Commands
  10.     {
  11.         private const string LINE_TYPE_FILE = @"C:\Program Files\Autodesk\AutoCAD 2015\linetypes\acad";
  12.  
  13.         [CommandMethod("SETLAYERCOLOR")]
  14.         public static void SetLayerColor()
  15.         {
  16.             var db = Application.DocumentManager.MdiActiveDocument.Database;
  17.  
  18.             var layerStructs = new List<LayerStruct>
  19.             {
  20.                 new LayerStruct() {Name = "G-ANNO-REFR", Color = 15, LineType = "BORDERX2"},
  21.                 new LayerStruct() {Name = "M-ANNO-TTLB", Color = 7, LineType = "BORDER2"},
  22.                 new LayerStruct() {Name = "M-ANNO-TEXT-NEWW", Color = 7, LineType = "BORDER"},
  23.             };
  24.  
  25.             using (var tr = db.TransactionManager.StartTransaction())
  26.             {
  27.                 var lineTypeTable = (LinetypeTable) tr.GetObject(db.LinetypeTableId, OpenMode.ForRead);
  28.                 var layerTable = (LayerTable) tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
  29.  
  30.                 foreach (var layerStruct in layerStructs)
  31.                 {
  32.                     if (!lineTypeTable.Has(layerStruct.LineType))
  33.                         db.LoadLineTypeFile(layerStruct.LineType, LINE_TYPE_FILE);
  34.  
  35.                     if (!layerTable.Has(layerStruct.Name))
  36.                     {
  37.                         var layer = new LayerTableRecord
  38.                         {
  39.                             Name = layerStruct.Name,
  40.                             Color = Color.FromColorIndex(ColorMethod.ByAci, layerStruct.Color),
  41.                             LinetypeObjectId = lineTypeTable[layerStruct.LineType]
  42.                         };
  43.  
  44.                         layerTable.Add(layer);
  45.                         tr.AddNewlyCreatedDBObject(layer, true);
  46.                     }
  47.                     else
  48.                     {
  49.                         var layer = (LayerTableRecord) tr.GetObject(layerTable[layerStruct.Name], OpenMode.ForWrite);
  50.                         layer.Color = Color.FromColorIndex(ColorMethod.ByAci, layerStruct.Color);
  51.                         layer.LinetypeObjectId = lineTypeTable[layerStruct.LineType];
  52.                     }
  53.                 }
  54.  
  55.                 tr.Commit();
  56.             }
  57.         }
  58.     }
  59.  
  60.     public struct LayerStruct
  61.     {
  62.         public string Name { get; set; }
  63.         public short Color { get; set; }
  64.         public string LineType { get; set; }
  65.     }
  66. }
Revit 2019, AMEP 2019 64bit Win 10

JONTHEPOPE

  • Guest
Re: eDuplicateRecordName
« Reply #5 on: August 08, 2014, 11:34:56 AM »
I appreciate the push, I stand corrected.   
I was never one to turn away someone who asked for help.

« Last Edit: August 08, 2014, 06:48:38 PM by Kerry »

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: eDuplicateRecordName
« Reply #6 on: August 11, 2014, 10:36:59 PM »
I'm picky on redundant code paths.  Fetch the layer or create it then set the properties. 
It's frustrating when I make a change but things aren't acting as expected because there needed to be an identical task modified in a separate code path. 

This works every time, all the time.  Just because someone won't do the the work for you and post the answer, don't assume it cannot be done.  A little better understanding of the .NET language and a little more research into the AutoCAD API would've gotten you the answers you were looking for.  To think that any Autodesk script file could out perform the .NET platform is ignorant.

Code - C#: [Select]
  1. using System.Collections.Generic;
  2. using Autodesk.AutoCAD.Colors;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  6.  
  7. namespace LayerLibrary
  8. {
  9.     public class Commands
  10.     {
  11.         private const string LINE_TYPE_FILE = @"C:\Program Files\Autodesk\AutoCAD 2015\linetypes\acad";
  12.  
  13.         [CommandMethod("SETLAYERCOLOR")]
  14.         public static void SetLayerColor()
  15.         {
  16.             var db = Application.DocumentManager.MdiActiveDocument.Database;
  17.  
  18.             var layerStructs = new List<LayerStruct>
  19.             {
  20.                 new LayerStruct() {Name = "G-ANNO-REFR", Color = 15, LineType = "BORDERX2"},
  21.                 new LayerStruct() {Name = "M-ANNO-TTLB", Color = 7, LineType = "BORDER2"},
  22.                 new LayerStruct() {Name = "M-ANNO-TEXT-NEWW", Color = 7, LineType = "BORDER"},
  23.             };
  24.  
  25.             using (var tr = db.TransactionManager.StartTransaction())
  26.             {
  27.                 var lineTypeTable = (LinetypeTable) tr.GetObject(db.LinetypeTableId, OpenMode.ForRead);
  28.                 var layerTable = (LayerTable) tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
  29.  
  30.                 foreach (var layerStruct in layerStructs)
  31.                 {
  32.                     if (!lineTypeTable.Has(layerStruct.LineType))
  33.                         db.LoadLineTypeFile(layerStruct.LineType, LINE_TYPE_FILE);
  34. // Start changes
  35.                     LayerTableRecord layer;
  36.                     if (!layerTable.Has(layerStruct.Name))
  37.                     {
  38.                         layer = new LayerTableRecord();
  39.                         layerTable.Add(layer);
  40.                         tr.AddNewlyCreatedDBObject(layer, true);
  41.                     }
  42.                     else
  43.                     {
  44.                         layer = (LayerTableRecord) tr.GetObject(layerTable[layerStruct.Name], OpenMode.ForWrite);
  45.                     }
  46.                     layer.Color = Color.FromColorIndex(ColorMethod.ByAci, layerStruct.Color);
  47.                     layer.LinetypeObjectId = lineTypeTable[layerStruct.LineType];
  48. // End change
  49.                 }
  50.  
  51.                 tr.Commit();
  52.             }
  53.         }
  54.     }
  55.  
  56.     public struct LayerStruct
  57.     {
  58.         public string Name { get; set; }
  59.         public short Color { get; set; }
  60.         public string LineType { get; set; }
  61.     }
  62. }


JONTHEPOPE

  • Guest
Re: eDuplicateRecordName
« Reply #7 on: August 15, 2014, 07:49:06 PM »
how could someone add in more properties?

Code - C#: [Select]
  1. ///   layer.LineWeight = layerStruct.double
  2. ///   layer.Id= Description, " text"
  3.  
« Last Edit: August 18, 2014, 09:44:49 AM by JONTHEPOPE »

JONTHEPOPE

  • Guest
Re: eDuplicateRecordName
« Reply #8 on: August 18, 2014, 09:43:24 AM »
Trying to change more properties I guess the plotstyle line weight has it's own xrecord.
there is some error cant convert double to a string?
I cant seem to find the dictonary for changing properties of a line type.

Code - C#: [Select]
  1.  
  2.            [CommandMethod("SETLAYERCOLOR")]
  3.         public static void SetLayerColor()
  4.         {
  5.             var db = Application.DocumentManager.MdiActiveDocument.Database;
  6.            
  7.             var layerStructs = new List<LayerStruct>
  8.             {
  9.                 new LayerStruct() {Name = "G-ANNO-REFR", Color = 15, LineType = "BORDERX2", Description = "rrr", LineWeight = 0.05},
  10.                 new LayerStruct() {Name = "M-ANNO-TTLB", Color = 7, LineType = "BORDER2", Description = "www", LineWeight = 0.05},
  11.                 new LayerStruct() {Name = "M-ANNO-TEXT-NEWW", Color = 7, LineType = "BORDER", Description = "rrr", LineWeight = 0.05},
  12.             };
  13.  
  14.             using (var tr = db.TransactionManager.StartTransaction())
  15.             {
  16.                 var lineTypeTable = (LinetypeTable) tr.GetObject(db.LinetypeTableId, OpenMode.ForRead);
  17.                 var layerTable = (LayerTable) tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
  18.  
  19.                 foreach (var layerStruct in layerStructs)
  20.                 {
  21.                     if (!lineTypeTable.Has(layerStruct.LineType))
  22.                         db.LoadLineTypeFile(layerStruct.LineType, LINE_TYPE_FILE);
  23.  
  24.                     if (!layerTable.Has(layerStruct.Name))
  25.                     {
  26.                         var layer = new LayerTableRecord
  27.                         {
  28.                             Name = layerStruct.Name,
  29.                             Color = Color.FromColorIndex(ColorMethod.ByAci, layerStruct.Color),
  30.                             LinetypeObjectId = lineTypeTable[layerStruct.LineType],
  31.                             Description = layerStruct.Description,
  32.                             LineWeight = layerStruct.LineWeight,
  33.                            
  34.                         };
  35.  
  36.                         layerTable.Add(layer);
  37.                         tr.AddNewlyCreatedDBObject(layer, true);
  38.                      
  39.                     }
  40.                    
  41.                     else
  42.                    
  43.                     {
  44.                         var layer = (LayerTableRecord) tr.GetObject(layerTable[layerStruct.Name], OpenMode.ForWrite);
  45.                         layer.Color = Color.FromColorIndex(ColorMethod.ByAci, layerStruct.Color);
  46.                         layer.LinetypeObjectId = lineTypeTable[layerStruct.LineType];
  47.                         layer.Description = layerStruct.Description;
  48.                         layer.LineWeight = layerStruct.LineWeight;
  49.  
  50.                     }
  51.                     db.LineWeightDisplay = true;
  52.                 }
  53.  
  54.                 tr.Commit();
  55.             }
  56.         }
  57.     }
  58.  
  59.     public struct LayerStruct
  60.     {
  61.         public string Name { get; set; }
  62.         public short Color { get; set; }
  63.         public string LineType { get; set; }
  64.         public string Description { get; set; }
  65.         public string LineWeight { get; set; }
  66.     }  
  67.  
  68. }
« Last Edit: August 19, 2014, 06:07:12 PM by JONTHEPOPE »

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: eDuplicateRecordName
« Reply #9 on: August 20, 2014, 12:25:32 PM »
First, have a look inside AutoCAD itself and fiddle around with the line weights assigned to layers.  Note what you can and can't do (hint - try adding an arbitrary line weight) - that should give you a good clue as to how they are implemented.

Second, create a short dummy routine that collects existing layers with their settings.  With a breakpoint set, inspect the property types and values of layers with different settings.  That will give you a good starting point as to what each of them are and how the properties reflect what is in AutoCAD.

Third, look up those various types in the ObjectARX SDK help files.  Review the methods and properties, which indicate how they should be handled.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}