Hello experts,
I wanted to select the below shown point in my drawing view. I know it is the origin point (in assembly) but I jumped into drawing, what VBA code shall I use?
Kindly share your expertise. Thanks in Advance.
Hello experts,
I wanted to select the below shown point in my drawing view. I know it is the origin point (in assembly) but I jumped into drawing, what VBA code shall I use?
Kindly share your expertise. Thanks in Advance.
Hi,
Here are some examples to follow:
Solidworks online help:
https://help.solidworks.com/2022/english/api/sldworksapi/get_visible_components_and_entities_in_drawing_view_example_vb.htm
Codestack:
Hi, thanks for your reply.
I found the following code from your timeline.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swView As View
Dim swDrawing As DrawingDoc
Dim selMgr As SelectionMgr
Dim status As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDrawing = swModel
Set selMgr = swModel.SelectionManager
status = swModel.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
Set swView = selMgr.GetSelectedObject6(1, -1)
'Fetch visible Entities
Dim vComps As Variant
vComps = swView.GetVisibleComponents
Dim vComp As Variant
Dim comp As Component2
For Each vComp In vComps
Set comp = vComp
Dim vVertexes As Variant
vVertexes = swView.GetVisibleEntities2(comp, swViewEntityType_e.swViewEntityType_Vertex)
Dim vVertex As Variant
Dim swVertex As Vertex
Dim swEntity As Entity
Dim point As Variant
Dim modelToViewTransform As MathTransform
Dim modelToSketchTransform As MathTransform
Dim swScaleTransform As MathTransform
Dim swPositionTransform As MathTransform
Dim swSketch As Sketch
Set swSketch = swView.GetSketch
Dim swMathUtility As MathUtility
Set swMathUtility = swApp.GetMathUtility
Set modelToSketchTransform = swSketch.modelToSketchTransform
Set swScaleTransform = ScaleTransform(swView)
Set modelToViewTransform = swView.modelToViewTransform
Set swPositionTransform = PositionTransform(swView)
Debug.Print ""
Dim swMathPoint As MathPoint
For Each vVertex In vVertexes
Set swVertex = vVertex
point = swVertex.GetPoint
Set swMathPoint = swMathUtility.CreatePoint(point)
Set swMathPoint = swMathPoint.MultiplyTransform(modelToSketchTransform) 'Dimension to Part-Origin
Set swMathPoint = swMathPoint.MultiplyTransform(swScaleTransform)
Set swMathPoint = swMathPoint.MultiplyTransform(modelToViewTransform)
Debug.Print "TransformedMathpoint => " & swMathPoint.ArrayData(0) * 1000 & " | " & swMathPoint.ArrayData(1) * 1000
swVertex.Select4 True, Nothing
Next vVertex
Next vComp
'swModel.ClearSelection2 True
End Sub
Public Function ScaleTransform(swView As SldWorks.View) As SldWorks.MathTransform
Dim swMathUtil As SldWorks.MathUtility
Dim transformData(15) As Double
Set swMathUtil = swApp.GetMathUtility
transformData(0) = 1#
transformData(1) = 0#
transformData(2) = 0#
transformData(3) = 0#
transformData(4) = 1#
transformData(5) = 0#
transformData(6) = 0#
transformData(7) = 0#
transformData(8) = 1#
transformData(9) = 0#
transformData(10) = 0#
transformData(11) = 0#
transformData(12) = swView.ScaleDecimal
transformData(13) = 0#
transformData(14) = 0#
transformData(15) = 0#
Set ScaleTransform = swMathUtil.CreateTransform(transformData)
End Function
Public Function PositionTransform(swView As SldWorks.View) As SldWorks.MathTransform
Dim swMathUtil As SldWorks.MathUtility
Dim transformData(15) As Double
Set swMathUtil = swApp.GetMathUtility
Dim pos As Variant
pos = swView.GetViewXform
transformData(0) = pos(0)
transformData(1) = pos(1)
transformData(2) = pos(2)
transformData(3) = pos(3)
transformData(4) = pos(4)
transformData(5) = pos(5)
transformData(6) = pos(6)
transformData(7) = pos(7)
transformData(8) = pos(8)
transformData(9) = pos(9)
transformData(10) = pos(10)
transformData(11) = pos(11)
transformData(12) = pos(12)
transformData(13) = 0#
transformData(14) = 0#
transformData(15) = 0#
Set PositionTransform = swMathUtil.CreateTransform(transformData)
End Function
My question is same. This works fine in a part drawing. I’ve an assembly, in this case how can I get the TransformedMathPoint of the vertices? Kindly guide me. Thanks
ah, ok - then you need to iterate over the visible drawing components of your drawing und process the transformation of every component. I didn’t tested the code below but it should something like this:
if (_swView.GetVisibleDrawingComponents() is object[] visibleDrawingComponents)
{
foreach (DrawingComponent component in visibleDrawingComponents.Cast<DrawingComponent>())
{
if (component == null)
{
_Logger.Warn("Casting to DrawingComponent failed");
continue;
}
var vertizes = GetVisibleEntities(component.Component, swViewEntityType_e.swViewEntityType_Vertex);
if(vertex!= null)
{
foreach (var vertex in vertizes.OfType<Vertex>())
{
var vPoint = vertex.getPoint() as double[];
var transformedPoint = TransformCoordinates(vPoint, component);
}
}
}
private double[] TransformCoordinates(double[] point, Component2 comp )
{
try
{
if (point == null || comp == null)
{
return Array.Empty<double>();
}
var swMathUtility = _swApp.GetMathUtility() as MathUtility;
var swMathpoint = swMathUtility .CreatePoint(point) as MathPoint;
swMathpoint = swMathpoint.MultiplyTransform(comp.Transform2) as MathPoint;
swMathpoint = swMathpoint.MultiplyTransform(_modelToView) as MathPoint;
return swMathpoint.ArrayData as double[];
}
catch (Exception ex)
{
_Logger.Error(ex, "Transformation failed: {Message}", ex.Message);
return Array.Empty<double>();
}
}