Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

get object from reference vba/c#

Status
Not open for further replies.

bipbophop

Computer
Sep 18, 2020
5
FR
Hello,

I'm currently struggling to access constraint references. I'm trying to find the object a reference is pointing to (see => GetConstraintElement).
Seems like the only usefull information in the reference object is DisplayName which is apparently a path in the specification tree... but I cannot find any way to get a reference to the pointed object.
Research didn't bring any usefull answer either. The closest I found was which is exactly the same issue. Unfortunately the answer doesn't work. Going through the hierarchy of products doesn't work because the syntax of the path is very complex and can contains weird element related to... whatever.
for example:
"Product1/Cube.2/!Product1/Cube.2/"
"Product1/Cube.1/!Selection_FVertex:(Vertex:(Neighbours:(Face:(Brp:(Pad.1;0:(Brp:(Sketch.1;3)));None:();Cf12:());Face:(Brp:(Pad.1;2);None:();Cf12:());Face:(Brp:(Pad.1;0:(Brp:(Sketch.1;2)));None:();Cf12:()));Cf12:());Pad.1_ResultOUT;Z0;G8782)"

Does anyone have any clue how to get the object pointed by references?
 
Replies continue below

Recommended for you

Hi.

Great that you've found similar thread. I've just updated it with the answer.
 
Hey,

thanks for your answer.
Sadly that doesn't work either. When enumerating constraints I'm in the context of the assembly, so not too sure how to get the part to get the HybridShapeFactory.
I tried on all the part in the assembly but all I get when calling GSMGetObjectFromReference is an exception with E_FAIL.
 
Oh, I see.
I'd try to parse reference path and extract part instance from there. Basically you want to traverse the assembly from root down to the first occurrence of exclamation sign (!).

And once you get to "Selection_" chunk pass it to Part.CreateReferenceFromName function to create a new reference.

Finally, pass new reference to GSMGetObjectFromReference to get a feature.
 
Seems to make sense.
However it still doesn't work.
I successfully get a reference when calling CreateReferenceFromName, but the call to GSMGetObjectFromReference fails with E_FAIL once again.
 
Sure, here it is:
Code:
foreach (MECMOD.Constraint c in prod.Connections("CATIAConstraints"))
{
	var elem1 = c.GetConstraintElement(1);
	var path = elem1.DisplayName.Split('/').TakeWhile(x => !x.StartsWith("!"));
	var current = prod.Application.Documents.OfType<ProductDocument>().First(x => prod.get_Name() == path.First()).Product;
	foreach (var token in path.Skip(1))
	{
		current = current.Products.Item(token);
	}

	string localreference = elem1.DisplayName.Substring(elem1.DisplayName.IndexOf('!'));
	var reference = currentpartdoc.Part.CreateReferenceFromName(localreference);
	HybridShapeFactory hsf = currentpartdoc.Part.HybridShapeFactory as HybridShapeFactory;
	var shape = hsf.GSMGetObjectFromReference(reference);
}
 
Thanks.

As far as I can see your code works fine. You retrieve constrained element which is not a shape, but a generative geomerty (e.g. face, vertex or edge) and it's a legitimate element to have.

To move from reference to shape process reference via Selection:

Code:
Selection.Add(reference);
Reference selRef = (Reference)Selection.Item(1).Value;
var shape = (HybridShape)selRef.Parent;
 
Okay, so in the end it was almost it.
selfRef.Parent is not a HybridShape but a SketchBasedShape (a Pad to be exact). To get the real type of the object I used IDisplatch.GetTypeInfo, then ITypeInfo.GetDocumentation, which gives the name of the real type.

Now I completely overlooked the fact that reference has derived types that are Edge/Face/Vertex (derived from Boundary). So I can actually cast it to those types.
However the reason why we have to get the reference through the selection is a mystery to me... If I try to IDisplatch.GetTypeInfo on elem1 directly I get a System.NotImplementedException, and if I try to cast it to Boundary I get an error as well, which is weird since its the only derived type.

Anyway thanks a lot, definitely couldn't have made it without your help!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top