Apologies in advance, I might have some of the terminology wrong here. I'm new to object-oriented programming.
I'm trying to control Siemens Solid Edge ST2 (a CAD/CAM program) from MATLAB via the COM API. when doing this from VB6 or VB.NET, it's pretty easy to identify (say), all the objects of class "objEdge" by doing some sort of for each loop through the objEdges collection object:
Dim objEdges As SolidEdgeGeometry.Edges objEdges = objCurve3D.GetType().InvokeMember("Edges", _ Reflection.BindingFlags.GetProperty, Nothing, objCurve3D, args) For Each objEdge In objEdges objProf.IncludeEdge(objEdge) Next
I'm trying to do this same thing from MATLAB. I successfully load the Solid Edge COM server using
h = actxserver('SolidEdge.Application'), can do things like open documents, create geometry, etc. through the API, but I am just not sure the MATLAB equivalent of this Visual Basic "object for-each" (for lack of a better name).
It looks like user Julian on the MATLAB Central forums here has the same problem, but they don't have an answer for him. Note that this is not the same question as this, which is looking at for-each loops over standard arrays, not COM objects.
Thanks very much for any help people can provide.
Here I'm wondering how to do the loop at all, when the data you are trying to loop over is a COM object collected inside another COM object. (i.e. what is the equivalent syntax of VB's "For Each X in Y" - Geoff Olynyk 2012-04-03 20:09
I know I shouldn't be answering my own question, but I think I cracked this one last night and figured it's better to post it here for others.
The VB.NET "For Each Object In Object" type of loop is, as far as I can tell, a shorthand for looping through the elements of a container object, or as Microsoft calls it, a "collection class". See MSDN references 1, 2. (edit: Can't post link to third MSDN page (for
Item() method) because I don't have enough karma to post more than two hyperlinks. You can get to it from the collections class page linked earlier.)
These objects, which hold collections of other objects, always have an attribute (in MATLAB-speak, a field)
Count, and a method
Item(k), which returns the k'th object in the collection. So object Cell would have collection class Cells, object Line2D would have collection class Lines2D, or in my case, within a 3D curve in the Solid Edge COM API, each Edge object is collected in the Edges collection class (container).
So to replicate the behaviour in MATLAB, which doesn't have this shorthand, you just have to use the
Count field and
Item() method directly. (This example code below uses a method
IncludeEdge() on the 2D sketch (Profile) object
objProfile to project each part (Edge) of a 3D curve to a 2D sketch.)
objEdges = objCurve.Edges(1) ; % get handle for Edges container class numEdges = objEdges.Count ; % number of edges in curve for k = 1:NumEdges, ; % loop through each Edge object in the curve objProfile.IncludeEdge(objEdges.Item(k)); % project current edge to sketch end
One thing that I'm not sure of is if every COM API uses this Microsoft convention of having a
Count attribute and an
Item() method, or if the names might change. But this appears to resolve my problem.