TheSwamp

Code Red => .NET => Topic started by: Jeff_M on July 22, 2019, 01:22:55 PM

Title: Calling a function from WPF form causes System.ArgumentException
Post by: Jeff_M on July 22, 2019, 01:22:55 PM
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.  
Title: Re: Calling a function from WPF form causes System.ArgumentException
Post by: Jeff_M 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.  
Title: Re: Calling a function from WPF form causes System.ArgumentException
Post by: MickD on July 22, 2019, 04:44:07 PM
 :-)
Title: Re: Calling a function from WPF form causes System.ArgumentException
Post by: Greg B on July 23, 2019, 09:37:09 AM
I was going to suggest that solution.
Title: Re: Calling a function from WPF form causes System.ArgumentException
Post by: MexicanCustard 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. : :-)
Title: Re: Calling a function from WPF form causes System.ArgumentException
Post by: nobody 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!
Title: Re: Calling a function from WPF form causes System.ArgumentException
Post by: CADbloke 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.
Title: Re: Calling a function from WPF form causes System.ArgumentException
Post by: gile 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. }