Author Topic: Calling a function from WPF form causes System.ArgumentException  (Read 323 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 3942
  • C3D user & customizer
I've come across an issue that has me quite perplexed. I have a function to create a profile in Civil 3D which has a number of arguments expected to be passed to it. For testing purposes, in that function is also code to create new variables with the same objects as those arguments and a call to the C3D function to create the profile. I also have 2 commands, one that calls the function directly with null objects (as I only want it to use those for testing in the function) and the other that opens a WPF form filled with objects to select which are then passed to the function.

When I use the testing command, the profile is created as expected. When I use the command with the form, it errors out with the System.ArgumentException even when I am having it only use the testing portion of the function which uses none of the passed arguments.

So, what could be causing the WPF form to wreak havoc with this function? Here's a snip of the code:

Code - C#: [Select]
  1.                 PromptEntityOptions opt = new PromptEntityOptions("\nSelect an Alignment");
  2.                 opt.SetRejectMessage("\nObject must be an alignment.\n");
  3.                 opt.AddAllowedClass(typeof(Alignment), true);
  4.                 ObjectId testAlignID = ed.GetEntity(opt).ObjectId;
  5.                 Alignment oAlignment = trans.GetObject(testAlignID, OpenMode.ForRead) as Alignment;
  6.                 ObjectId testLayerID = oAlignment.LayerId;
  7.                 ObjectId testSurfaceId = doc.GetSurfaceIds()[0];
  8.                 ObjectId testStyleId = doc.Styles.ProfileStyles[0];
  9.                 ObjectId testLabelSetId = doc.Styles.LabelSetStyles.ProfileLabelSetStyles[0];
  10.                 //--------------------------------------------------------------------------------------------------
  11.                 try
  12.                 {
  13.                     //The following line works when function is called directly from a command, fails when called from the WPF window.
  14.                     ObjectId otherprofileId = Profile.CreateFromSurface("Other Profile", testAlignID, testSurfaceId, testLayerID, testStyleId, testLabelSetId);
  15.  

Jeff_M

  • King Gator
  • Posts: 3942
  • C3D user & customizer
Re: Calling a function from WPF form causes System.ArgumentException
« Reply #1 on: July 22, 2019, 01:39:13 PM »
Of course I discover the solution almost as soon as I post this. Once I added the following it works as it should.

Code - C#: [Select]
  1.             using (DocumentLock dl = doc.LockDocument())
  2.             {
  3.                  ...the code to create the profile here
  4.              }
  5.  

MickD

  • Gator
  • Posts: 3289
  • (x-in)->[process]->(y-out)
Re: Calling a function from WPF form causes System.ArgumentException
« Reply #2 on: July 22, 2019, 04:44:07 PM »
 :-)
Debugging:
Being the detective in a crime movie where you're also the murderer.

“Someone's sitting in the shade today because someone planted a tree a long time ago.”
- Warren Buffet

Greg B

  • Seagull
  • Posts: 12318
  • Tell me a Joke!
Re: Calling a function from WPF form causes System.ArgumentException
« Reply #3 on: July 23, 2019, 09:37:09 AM »
I was going to suggest that solution.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Calling a function from WPF form causes System.ArgumentException
« Reply #4 on: July 25, 2019, 01:49:06 PM »
BTW,  it's not a WPF form.  It's a WPF window.  Semantics I know but using the word form with WPF makes my skin crawl. : :-)
Revit 2019, AMEP 2019 64bit Win 10

quaziconscience

  • Swamp Rat
  • Posts: 776
  • .net stuff
Re: Calling a function from WPF form causes System.ArgumentException
« Reply #5 on: July 26, 2019, 05:37:58 PM »
Of course I discover the solution almost as soon as I post this. Once I added the following it works as it should.

Code - C#: [Select]
  1.             using (DocumentLock dl = doc.LockDocument())
  2.             {
  3.                  ...the code to create the profile here
  4.              }
  5.  

bahahaha.... SOOOOOO glad I'm not the only one that happens to!

CADbloke

  • Bull Frog
  • Posts: 304
  • Crash Test Dummy
Re: Calling a function from WPF form causes System.ArgumentException
« Reply #6 on: August 08, 2019, 06:42:41 AM »
I just added that to a long-running project. :/ Long running ... it was git commit # 1.343. Exception handling kept it alive. I'm glad I log all exceptions

You are not alone.

gile

  • Water Moccasin
  • Posts: 2222
  • Marseille, France
Re: Calling a function from WPF form causes System.ArgumentException
« Reply #7 on: August 08, 2019, 07:09:17 AM »
Hi,

Document.DocumentLock() returns a DocumentLock instance which only provides a Dispose() method.
So, when called within a using statement, storing the return value of DocumentLock() in a variable seems to me absolutely useless.
By my side, I use:
Code - C#: [Select]
  1. using (doc.LockDocument())
  2. {
  3.     ...the code here
  4. }
Speaking English as a French Frog