Author Topic: arrays  (Read 10092 times)

0 Members and 1 Guest are viewing this topic.

JONTHEPOPE

  • Guest
arrays
« on: July 25, 2014, 12:25:46 AM »

How do I create a new array with linetypes and extract data from the slayernames array into the linetypes array.
Also am I trying to do too many things inside a method. Any help would save me from spontaneous combustion.

Code - C#: [Select]
  1.  
  2. [CommandMethod("assignlinetype")]
  3. public static void assignlinetype()
  4. {
  5.     Document acDoc = Application.DocumentManager.MdiActiveDocument;
  6.     Database acCurDb = acDoc.Database;
  7.  
  8.     using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  9.     {
  10.         LayerTable acLyrTbl;
  11.         acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
  12.                                         OpenMode.ForRead) as LayerTable;
  13.  
  14.         LinetypTable acLineTbl;
  15.         acLineTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
  16.                                         OpenMode.ForRead) as LinetypeTable;
  17.  
  18.         string[] acLinetypes = new string[3];
  19.         acLinetypes[0] = "hidden10"
  20.         acLinetypes[1] = "center10"
  21.         acLinetypes[2] = "dot10"
  22.  
  23.        foreach  (string linetype in acLinetypes)    
  24.          if (acLineTypTbl.Has(sLineTypName) == false)
  25.         {
  26.             acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
  27.         }
  28.  
  29. // how do I error handle this if true???
  30.  
  31.         string[] sLayerNames = new string[3];
  32.         sLayerNames[0] = "ACIRed";
  33.         sLayerNames[1] = "TrueBlue";
  34.         sLayerNames[2] = "ColorBookYellow";
  35.  
  36.         Color[] acColors = new Color[3];
  37.         acColors[0] = Color.FromColorIndex(ColorMethod.ByAci, 1);
  38.         acColors[1] = Color.FromColorIndex(ColorMethod.ByAci, 1);
  39.         acColors[2] = Color.FromColorIndex(ColorMethod.ByAci, 1);
  40.  
  41. // can I have an array here that references linetypes to the layers?
  42.                                    
  43.         int nCnt = 0;
  44.  
  45.         foreach (string sLayerName in sLayerNames)
  46.         {
  47.             if (acLyrTbl.Has(sLayerName) == false)
  48.             {
  49.                 using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
  50.                 {
  51.                     acLyrTblRec.Name = sLayerName;
  52.  
  53.                     if (acLyrTbl.IsWriteEnabled == false) acLyrTbl.UpgradeOpen();
  54.  
  55.                     acLyrTbl.Add(acLyrTblRec);
  56.                     acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  57.  
  58.                     acLyrTblRec.Color = acColors[nCnt];
  59.                  
  60.  
  61. // should there be a linetype reference here ?
  62.                      
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
  68.                                                                  OpenMode.ForWrite) as LayerTableRecord;
  69.  
  70.                 acLyrTblRec.Color = acColors[nCnt];
  71.  
  72. // should there be a linetype reference here do I load the linetype table record for write now ?
  73.  
  74.             }
  75.             nCnt = nCnt + 1;
  76.         }
  77.         acTrans.Commit();
  78.     }
  79. }

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: arrays
« Reply #1 on: July 28, 2014, 08:14:24 AM »
You could use a Dictionary<string,string> to map layer names to linetypes
Revit 2019, AMEP 2019 64bit Win 10

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: arrays
« Reply #2 on: July 28, 2014, 05:52:56 PM »
I'm, having a hard time wanting to help you here John, it seems that you haven't taken any time to look at the documentation to see that the LayerTableRecord has a LinetypeObjectId property which you need to set to provide your default behavior.

You could also solve this with an array of Tuple<string, int, ObjectId> to represent the layer name, color, and Id of LinetypeTableRecord since you are accessing these bits of data only once and sequentially. 

In more general terms Mr Custard is absolutely right to use the dictionary

Locke

  • Guest
Re: arrays
« Reply #3 on: July 29, 2014, 08:52:07 AM »
I'm, having a hard time wanting to help you here John, it seems that you haven't taken any time to look at the documentation...

Hey at least he's posting code, and giving it a shot.  8-)

John, when things start to appear super complicated, it usually means there's something you're missing.  Another idea for what you're trying to accomplish is creating your own class.  Then you can just store them in a normal array/list.

One last tiny pet peeve... When dealing with booleans, stop re-evaluating if they're true or false.  Boolean functions are already evaluated to be either true or false, therefore a comparison is just wasted cycles.
Code - C#: [Select]
  1. // This...
  2. if (acLineTypTbl.Has(sLineTypName) == false)
  3. {
  4.     acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
  5. }
  6.  
  7. // Becomes this:
  8. if (!acLineTypTbl.Has(sLineTypName))
  9. {
  10.     acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
  11. }
  12.  

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: arrays
« Reply #4 on: July 29, 2014, 01:29:02 PM »
Hey at least he's posting code, and giving it a shot.  8-)

True, but my frustration comes that instead of helping people to understand the complexities of the system and the language we're holding hands and teaching them how to walk.  Most of us here have spend hundreds or thousands of hours researching and trying to reach the point where we can give the help that is provided here. 

It seems to me that it's a bit of a slap in the face to come here looking for others to spell this out rather than doing some research.  Especially from my own personal experience: I had been working with autocad for nearly a year before my questions were complex enough that I wasn't able to find the answer. There are not many questions which haven't already been asked right here on this board.

That said, I would argue that teaching the noobs how to answer their questions is teaching them more than just answering their questions  :-D

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: arrays
« Reply #5 on: July 29, 2014, 01:48:35 PM »
Patience you .net-ers. I'm on chapter nine of my c# book so I will be here to help with the non-api questions soon ...I hope.

BTW, I'm not really getting a big warm fuzzy over this C# language yet (very clumsy so far).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: arrays
« Reply #6 on: July 29, 2014, 02:44:13 PM »
Hey at least he's posting code, and giving it a shot.  8-)

True, but my frustration comes that instead of helping people to understand the complexities of the system and the language we're holding hands and teaching them how to walk.  Most of us here have spend hundreds or thousands of hours researching and trying to reach the point where we can give the help that is provided here. 

It seems to me that it's a bit of a slap in the face to come here looking for others to spell this out rather than doing some research.  Especially from my own personal experience: I had been working with autocad for nearly a year before my questions were complex enough that I wasn't able to find the answer. There are not many questions which haven't already been asked right here on this board.

That said, I would argue that teaching the noobs how to answer their questions is teaching them more than just answering their questions  :-D

Shouldn't be any problems helping noobs, but I do agree that giving a man a fish only results in him asking for tartar sauce (and breath mints).  Most of the frustration come when jumping over the underlying concepts.  In this case, the documentation for LayerTableRecord class as well as the various means of effectively managing list-type information (List, Dictionary, Collection, etc.).

Its a fine line between telling them to RTFM, and giving them complete finished code (which will likely fail on them when its copy/pasted out of context leading to more questions rather than understanding).  I usually get around that by going into Stupid Question mode ie. asking a few relevant questions which *should* prompt some research on their part, hopefully leading to learning and not just read/copy/paste.
If you are going to fly by the seat of your pants, expect friction burns.

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: arrays
« Reply #7 on: July 29, 2014, 04:04:42 PM »
Patience you .net-ers. I'm on chapter nine of my c# book so I will be here to help with the non-api questions soon ...I hope.

BTW, I'm not really getting a big warm fuzzy over this C# language yet (very clumsy so far).

Which book John ?

Clumsy gets better with practice ;-D
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: arrays
« Reply #8 on: July 29, 2014, 05:50:23 PM »
The book is called: "Programming C#: Building .NET Applications With C#"
It's a pretty good book.

:) Yes, but the inheritance thing is killing me. The book seems to be geared more for an introduction to programming but I'm rolling with it so far. I really need to get a compiler installed so I can actually run some of this code instead of accepting it at face value but I plan on going back and re-reading and or reading another book so it's all good.

Question (off topic a little): is this kind of for statement legal in C# (the code in the OP is bothering me). -i.e. Why use a foreach when a for statement will be much cleaner.
Code - C++: [Select]
  1. for ( int i = 0, int j = 0 ; i < count ; ++i, ++j ) {
  2.     // my code
  3. }
  4.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: arrays
« Reply #9 on: July 29, 2014, 06:11:54 PM »
The book is called: "Programming C#: Building .NET Applications With C#"
It's a pretty good book.

:) Yes, but the inheritance thing is killing me. The book seems to be geared more for an introduction to programming but I'm rolling with it so far. I really need to get a compiler installed so I can actually run some of this code instead of accepting it at face value but I plan on going back and re-reading and or reading another book so it's all good.

Question (off topic a little): is this kind of for statement legal in C# (the code in the OP is bothering me). -i.e. Why use a foreach when a for statement will be much cleaner.
Code - C++: [Select]
  1. for ( int i = 0, int j = 0 ; i < count ; ++i, ++j ) {
  2.     // my code
  3. }
  4.  

Yes, with a small change ...

Code - C#: [Select]
  1. void Main()
  2. {
  3.         int count = 7;
  4.         for ( int i = 0, j = 10 ; i < count ; ++i, ++j ) {
  5.  
  6.         Console.WriteLine(i + ":" + j);
  7.         }
  8. }

Quote
0:10
1:11
2:12
3:13
4:14
5:15
6:16
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: arrays
« Reply #10 on: July 29, 2014, 06:18:38 PM »
Yes, with a small change ...

Code - C#: [Select]
  1. void Main()
  2. {
  3.         int count = 7;
  4.         for ( int i = 0, j = 10 ; i < count ; ++i, ++j ) {
  5.  
  6.         Console.WriteLine(i + ":" + j);
  7.         }
  8. }
… snip

Oh, nice! The compiler understands what type "J" is. …that's cool. Thank you.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: arrays
« Reply #11 on: July 29, 2014, 06:19:47 PM »
The book is called: "Programming C#: Building .NET Applications With C#"
It's a pretty good book.


Jesse Liberty does a good job I think

I really need to get a compiler installed so I can actually run some of this code instead of accepting it at face value but I plan on going back and re-reading and or reading another book so it's all good.



John; Have a look at Express 2013 for Windows Desktop
http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx
or Express 2013 for Windows, or Express 2013 for Web

Working in a decent IDE isn't a bad as you've been saying for the last 10 years ;-D

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: arrays
« Reply #12 on: July 29, 2014, 06:23:59 PM »

or worst case, there is a compiler installed with the .NET Framework

http://msdn.microsoft.com/en-us/library/78f4aasd.aspx
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: arrays
« Reply #13 on: July 29, 2014, 06:25:43 PM »
Visual studio will make you forget about VIM  :evil:



JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: arrays
« Reply #14 on: July 29, 2014, 06:26:36 PM »
LOL! :D
Ok I will but I planned on trying MonoDevelop as well so I can be cross-platform (No MS Windows at home) so I can't promise I'll stick with Visual Studio.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org