0

My application talks to another piece of software (Catia V6/3DEXPERIENCE), using various .dll's.

Some of the functionality in this interface appears to run slightly asynchronously, which is causing occasional failures in my script.

"AnyObject" and "Selection" types, along with their functions, come from the .dll; the "IsolateThis" function is embedded in my own class.

    public INFITF.AnyObject IsolateThis(INFITF.AnyObject thing)
    {
        Selection osel = catapp.ActiveEditor.Selection; //get a selection object
        osel.Clear(); //make sure nothing pre-selected
        osel.Add(thing); //add item to be copied to selection
        osel.Copy(); //copy
        osel.Clear(); //clear selection
        osel.Add(this.hbod); //add item to paste into
        osel.Paste(); //paste
        this.part.Update(); //update the parent object
        return (INFITF.AnyObject)osel.Item(1).Value; //return pasted result

    }

The function basically copies one object, pastes it into another, and returns the resulting pasted object. The Selection object is kind enough to update itself to only contain the pasted thing after you paste, which is why the last line works.

For some reason it fine when I step through the code, but if I try to use the returned object elsewhere in real-time then I sometimes get an invalid cast exception, for example on the following line:

CATCurve tmpcurve = new CATCurve((HybridShapeCurveExplicit)tmpbody.IsolateThis(this.Branch.shape));

Which does not recur when I hit F10 to continue. My suspicion is that it's firing a paste command to Catia, which takes time to complete, but is ploughing on with the rest of my code without requiring a response. This results in the wrong object being returned (as 'this.hbod' is still seen as the selected object, which is not compatible with the expected return object).

How can I wrap/refactor this to ensure that the paste and update operations have fully completed? Is this even possible with things drawn from external references?

I'm still pretty new to c#. I've tried looking into using Task and Process but to be honest, am quite lost as to how to implement them in this situation.

ErosRising
  • 165
  • 7
  • If this library provides events you can subscribe to (e.g. `OnPasteCompleted`) then you can wrap those callbacks in a `TaskCompletionSource` and then `await` them. – thisextendsthat Nov 09 '18 at 11:38
  • @thisextendsthat Is there a way to interrogate the library to find such events? Their Documentation is atrocious. – ErosRising Nov 09 '18 at 11:56
  • you'll just need to use intellisense I guess – thisextendsthat Nov 09 '18 at 12:04
  • I have no knowlodge of Catia V6 automation, but as far as I know Catia V5 the selection class does not support any kind of asynchronous operation. Are you sure it is always the same object you are sending the method? (Catia has indeed some weird behavior sometimes) – Quima Nov 09 '18 at 14:32
  • 1
    I don't know specifically about V6 but in V5 the paste command does not return the result object of the paste or put it into selection. You have to play games like 'result = targetGeoSet.HybridShapes.Item(targetGeoSet.HybridShapes.Count)' for example. – C R Johnson Nov 09 '18 at 17:04
  • @CRJohnson in V6 it seems to - when/if the paste actually works. I say IF because when I've tried an approach similar to what you suggest, sometimes the collection has not been updated in time and I get an invalid index (if it's the first object to be added to the body) or the wrong object returned (if there was already something in there) – ErosRising Nov 15 '18 at 09:05

0 Answers0