
Re: Point Transformation
Josh Brady Sep 11, 2015 1:06 PM (in response to Alfonso Cartagena)First get transform from source part to assembly. Multiply source coords by part to assy transform. (That's what you have so far). Then, get the same transform for the target part, invert transform, and mutiply.

Re: Point Transformation
Alfonso Cartagena Sep 11, 2015 2:45 PM (in response to Josh Brady)Josh,
Thank you for the help! I completely understand what I need to do when it comes to how it would work. I guess what I don't understand is how my code should reflect that. I put the section of code below and referenced where I am confused. How do I tell this code to transform the points into "Part2" for my operation?
Thank you,
Alfonso
Sub main() 'Connect the program to SolidWorks Set swApp = CreateObject("SldWorks.Application") Set Model = swApp.ActiveDoc 'Prepare the MathUtility Set MathUtil = swApp.GetMathUtility 'Get the SelectionMgr Set selMgr = Model.SelectionManager 'Get the sketch from the SelectionMgr boolstatus = Model.Extension.SelectByID2("Sketch6@Part11@Assem1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0) Set SketchFeature = selMgr.GetSelectedObject6(1, 0) Set SketchFeature = SketchFeature.GetSpecificFeature2 'Get the sketch points SketchPoints = SketchFeature.GetSketchPoints2 'Build a coordinate array from the first point in the sketch PointCoords(0) = SketchPoints(0).X PointCoords(1) = SketchPoints(0).Y PointCoords(2) = SketchPoints(0).Z 'Create the new MathPoint from the sketch point data. 'MathP refers to the point location in the sketch coordinates Set MathP = MathUtil.CreatePoint(PointCoords) 'Display the point coordinates in relation to the sketch origin SketchPoints = MathP.ArrayData MsgBox SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2) RIGHT HERE IS WHERE I'M LOST. 'Get the modeltosketch transform for this sketch Set MathTrans = SketchFeature.ModelToSketchTransform 'Get the inversion of the transform Set MathTrans = MathTrans.Inverse 'Multiply the point by the inverse transform 'MathP now refers to the point location in the model coordinates Set MathP = MathP.MultiplyTransform(MathTrans) 'Display the point coordinates in relation to the model origin SketchPoints = MathP.ArrayData Debug.Print "Sketch point x, y and z coordinates: "; SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2); "" Debug.Print " " MsgBox SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2)


Re: Point Transformation
Josh Brady Sep 11, 2015 10:06 PM (in response to Alfonso Cartagena)Based on the point where you said you're lost... A transformation matrix (or transform) is a mathematical construct that contains instructions on how to translate Euclidean geometry elements between different coordinate systems. SolidWorks can generate this transform based on its knowledge about how the coordinate systems are positioned/rotated relative to one another.
Sketches have their own coordinate systems. Parts have their own coordinate systems. Assemblies have their own coordinate systems. In order to find out the coordinates of a sketch point contained by one part, you have to get the path of transforms from where the point is to where you're interested in. Then, you multiply the point coordinates by the transforms in the correct order to get from where you started to where you want to end up.
Sometimes, the transform you can get from SolidWorks is the wrong direction. In your example above, there's a line that gets the "model to sketch" transform. However, you're already at the sketch, and you want to get to the model. Fortunately, switching the direction of the transform is as easy as getting the inverse of the transform.
If I read your question correctly, your total journey is (roughly) Sketch1@Part1→Part1→Assy1→Part2. Each of those arrows represents a multiplication of point data by a transform. Your example code shows the first one of these. You have to get 2 more. These will come from Component2 objects in the context of the assembly.

Re: Point Transformation
Alfonso Cartagena Sep 14, 2015 12:38 PM (in response to Josh Brady)Josh,
I understand what the code does. That's not my issue here.
Yes, you have the sequence of events in the correct order. At the beginning of this code the sketch is specifically called out and the points are gathered. Going through the next part of code what's confusing to me is that there nothing in the code specifying "Part1" as the ending result. I assumed the coordinates were transformed to the assembly. Once that is established my next issue arises, due to multiple parts within the assembly, how do I call out which part the points are to be transformed to?
Sorry for all the confusion, I am new to API.
Thank you,
Alfonso

Re: Point Transformation
Josh Brady Sep 14, 2015 1:18 PM (in response to Alfonso Cartagena)SketchFeature.ModelToSketchTransform is what specifies the end result being in the part model's context. The sketch knows what part model it's in. That's why I said you need to get the transform from part to assembly. As far as how to pick the part in the assembly you want to transform to... There are so many ways. Just depends on how your code can know.
Sorry... Not trying to be a jerk here, but it sounds like you're further over your head than I have time to teach at the moment.

