Author Topic: Get line lengths  (Read 3194 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Get line lengths
« on: May 22, 2015, 06:48:12 AM »
Hello guys ,

I am having an error with the variable len just inside the foreach function , can anyone tell me why I have this error ?

Thanks

Code - C#: [Select]
  1. [CommandMethod ("getlen")]
  2.         public static void LineLengths()
  3.         {
  4.             Document Adoc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = Adoc.Database;
  6.             Editor ed = Adoc.Editor;
  7.             double len;            
  8.             ed.WriteMessage("\nSelect Lines: ");
  9.  
  10.             TypedValue[] Tvals = new TypedValue[] { new TypedValue(0, "LINE") };
  11.             SelectionFilter ssf = new SelectionFilter(Tvals);
  12.             PromptSelectionOptions opt = new PromptSelectionOptions();
  13.             PromptSelectionResult psr = ed.GetSelection(opt, ssf);
  14.          
  15.             if (psr.Status == PromptStatus.OK && psr.Value.Count > 0)
  16.                 using (Transaction trans = db.TransactionManager.StartTransaction())
  17.                 {
  18.                     foreach (SelectedObject item in psr.Value)
  19.                     {                
  20.                         Line line = (Line)trans.GetObject(item.ObjectId, OpenMode.ForWrite);
  21.  
  22.                         len += line.Length; /// <-- this len variable shows as an error
  23.                     }
  24.                     ed.WriteMessage("Lengths of line(s) :" + len.ToString());
  25.                     trans.Commit();
  26.                 }
  27.         }

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get line lengths
« Reply #1 on: May 22, 2015, 07:10:07 AM »
Go into debug mode, put a breakpoint at the line = statement, check your variable values as you iterate the psr .. that may help.

BTW .
A cheaper process is to declare the line variable outside the loop and just assign a value as you iterate rather than declare and assign at each step

... and more importantly ... why are you forcing the getObject to a Line ... doesn't your selection process ensure that it is a line ??

added: and why are you setting OpenMode to ForWrite ??
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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Get line lengths
« Reply #2 on: May 22, 2015, 07:16:36 AM »
Hi,

When (or where) do you call tr.Commit() ?
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Get line lengths
« Reply #3 on: May 22, 2015, 07:28:54 AM »
Hello Kerry ,
I am assigning a new line object in the looping process to get length property and that is the only way that I know for far .

Hello gile,
I know that the commit is not need in this program because we have no new objects added by these codes.

And the reason why it was there is that I started writing the program from another program and forgot to remove it from the new program .

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get line lengths
« Reply #4 on: May 22, 2015, 07:36:19 AM »
Coder,
You are incorrect on both counts.
Read again exactly what I wrote. There is a distinction between multiple assigning and multiple declaring/assigning.

Commit IS advantageous .. it is faster and safer. this has been the cause of many long discussions here and elsewhere over the last 10 or so years.
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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Get line lengths
« Reply #5 on: May 22, 2015, 08:32:41 AM »
Coder,

First, forgive my reply, I misread your code.

Second, follow Kerry's advices.

Third, let your cursor on the len variable (in the line the error occurs), visual studio should display a tooltip which tells you what kind of error...
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Get line lengths
« Reply #6 on: May 22, 2015, 12:25:53 PM »
Hi,

I can not run the program because I receive a message  (There were build errors . Would you like to .... etc  Yes / No)

How to test the program in this case ?

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Get line lengths
« Reply #7 on: May 22, 2015, 12:33:27 PM »
You don't initialize the len symbol.
double len;

Change that to
double len= 0;

Coder

  • Swamp Rat
  • Posts: 827
Re: Get line lengths
« Reply #8 on: May 22, 2015, 12:49:07 PM »
You don't initialize the len symbol.
double len;

Change that to
double len= 0;

Hi Jeff_M
I have had a headache of this error since yesterday and now everything is settled down. :-D

Thank you so much .

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Get line lengths
« Reply #9 on: May 22, 2015, 12:49:54 PM »
Hi,

I can not run the program because I receive a message  (There were build errors . Would you like to .... etc  Yes / No)

How to test the program in this case ?

To do what I suggested, you do not need to run the program. Most "edition time" errors are shown by Visual Studio underlining the error with a wavy red line.
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Get line lengths
« Reply #10 on: May 22, 2015, 12:59:47 PM »
To do what I suggested, you do not need to run the program. Most "edition time" errors are shown by Visual Studio underlining the error with a wavy red line.

Thank you gile , now I know the differences between assigning and declaring .
Thanks to Kerry also .

You guys are wonderful .


Coder

  • Swamp Rat
  • Posts: 827
Re: Get line lengths
« Reply #11 on: May 22, 2015, 01:29:52 PM »
Go into debug mode, put a breakpoint at the line = statement, check your variable values as you iterate the psr .. that may help.
I could not get into debug mode due to the error that is in the program .
BTW .
A cheaper process is to declare the line variable outside the loop and just assign a value as you iterate rather than declare and assign at each step
It is outside the loop process , is not it ?

... and more importantly ... why are you forcing the getObject to a Line ... doesn't your selection process ensure that it is a line ??
I could not come with an alternative one than this one , can you offer your idea please ?

added: and why are you setting OpenMode to ForWrite ??
Yes it supposed to be ForRead and not ForWrite , you are right .

Thank you.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Get line lengths
« Reply #12 on: May 22, 2015, 10:49:29 PM »
It is outside the loop process , is not it ?

No it is not.  You are declaring a new line every time you iterate the for loop.  You should declare the line outside the loop and then set its value each time through the loop like Kerry said.  Something like below.

Code - C#: [Select]
  1.             if (psr.Status == PromptStatus.OK && psr.Value.Count > 0)
  2.                 using (Transaction trans = db.TransactionManager.StartTransaction())
  3.                 {
  4.                     Line line;
  5.                     foreach (SelectedObject item in psr.Value)
  6.                     {                
  7.                         line = (Line)trans.GetObject(item.ObjectId, OpenMode.ForWrite);
  8.  
  9.                         len += line.Length; /// <-- this len variable shows as an error
  10.                     }
  11.                     ed.WriteMessage("Lengths of line(s) :" + len.ToString());
  12.                     trans.Commit();
  13.                 }
  14.             }
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Coder

  • Swamp Rat
  • Posts: 827
Re: Get line lengths
« Reply #13 on: May 23, 2015, 02:08:56 AM »
Thank you Keith Brown , I got the point from your example .  :-)