The following code throws my exception when run from an Assembly.
Why does the ModelDoc2 not accept a persistreference3 that it just handed me?
const int PERSIST_REF_OBJ_OK = (int)swPersistReferencedObjectStates_e.swPersistReferencedObject_Ok;
public List<object> SegmentsToPersistRefs(List<SketchSegment> segments)
{
Debug.Print("SegmentsToPersistRefs called for list of " + segments.Count);
List<object> refs = new List<object>();
foreach (SketchSegment seg in segments)
{
object pid = mDoc.Extension.GetPersistReference3(seg);
if (pid == null)
{
throw new ApplicationException("SketchSegment GetPersistReference3 is null");
}
int errorCode;
object obj = mDoc.Extension.GetObjectByPersistReference3(pid, out errorCode);
if (errorCode != PERSIST_REF_OBJ_OK)
{
// *** ENDS UP HERE with errorCode = 1 == INVALID ***
throw new ApplicationException("Cannot get object from PersistRef3 we just got");
}
refs.Add(pid);
}
return refs;
}
Found the problem. I was using a part's ModelDoc2, which came from looking from a sketch up to its parent document. If I switch to the ModelDoc2 for the .SLDASM, the GetObjectByPersistReference3() works.
After opening my .SLDASM doc, there are three ModelDoc2 that can be found from SwApp.GetFirstDocument() and ModelDoc2.GetNext().
// ModelDoc2 GetPathName(): sled_01.SLDPRT
// next GetPathName(): test_part_03.SLDPRT
// next GetPathName(): sled_and_part.SLDASM
If I use the ModelDoc2 for the .SLDASM, everything works.