# Export to CNC/ISO

• posted
From little time I am trying to learn VB(A) and SoliWorks, my experience =
=20
it is based particularly on AutoLisp/AutoCAD. =20
I should create a program to export information in CNC/ISO language, =20
the parts to be examined are enough simple:
=20
- Panel (of wood) rectangular [Extrusion] =20
=20
- Hole [LibraryFeature] [Cut] [swEndCondBlind] [LPattern] =20
=20
- Hole [LibraryFeature] [Cut] [swEndCondThroughAll] [LPattern] =20
=20
- Hole [LibraryFeature] [Cut] [swEndCondBlind] [LPattern] =20
+[Chamfer] =20
=20
- Hole [LibraryFeature] [Cut] [swEndCondThroughAll] [LPattern] =20
+[Chamfer] =20
=20
All the holes are perpendicular to the surfaces of the panel.=20
=20
The informations I need are: =20
=20
- measures of the panel =20
=20
- holes: face, center, diameter, depth, chamfer, (lPattern?) =20
=20
I have taken I sprout from: =20
"Select Edges of All Holes on Face Example (VB)"
in SW API Help and I have written the program (see below). =20
=20
For the holes: it is possible to get the center of the hole and the =
face, =20
getting the rest of information from the [LibraryFeature]?
=20
My apoligies for my english.
Marco
[code]
Option Explicit
Function GetFaceNormalAtMidCoEdge(swCoEdge As SldWorks.CoEdge) As =
Variant
Dim swFace As SldWorks.Face2
Dim swSurface As SldWorks.Surface
Dim swLoop As SldWorks.Loop2
Dim varParams As Variant
Dim varPoint As Variant
Dim dblMidParam As Double
Dim dblNormal(2) As Double
Dim bFaceSenseReversed As Boolean
varParams =3D swCoEdge.GetCurveParams
If varParams(6) > varParams(7) Then
dblMidParam =3D (varParams(6) - varParams(7)) / 2 + varParams(7)
Else
dblMidParam =3D (varParams(7) - varParams(6)) /
2 + varParams(6)
End If
varPoint =3D swCoEdge.Evaluate(dblMidParam)
' Get the face of the given coedge
' Check for the sense of the face
Set swLoop =3D swCoEdge.GetLoop
Set swFace =3D swLoop.GetFace
Set swSurface =3D swFace.GetSurface
bFaceSenseReversed =3D swFace.FaceInSurfaceSense
varParams =3D swSurface.EvaluateAtPoint(varPoint(0), varPoint(1), =
varPoint(2))
If bFaceSenseReversed Then
' Negate the surface normal as it is opposite from the face =
normal
dblNormal(0) =3D -varParams(0)
dblNormal(1) =3D -varParams(1)
dblNormal(2) =3D -varParams(2)
Else
dblNormal(0) =3D varParams(0)
dblNormal(1) =3D varParams(1)
dblNormal(2) =3D varParams(2)
End If
GetFaceNormalAtMidCoEdge =3D dblNormal
End Function
Function GetTangentAtMidCoEdge(swCoEdge As SldWorks.CoEdge) As Variant
Dim varParams As Variant
Dim dblMidParam As Double
Dim dblTangent(2) As Double
varParams =3D swCoEdge.GetCurveParams
If varParams(6) > varParams(7) Then
dblMidParam =3D (varParams(6) - varParams(7)) / 2# + =
varParams(7)
Else
dblMidParam =3D (varParams(7) - varParams(6)) /
2# + =
varParams(6)
End If
varParams =3D swCoEdge.Evaluate(dblMidParam)
dblTangent(0) =3D varParams(3)
dblTangent(1) =3D varParams(4)
dblTangent(2) =3D varParams(5)
GetTangentAtMidCoEdge =3D dblTangent
End Function
Function GetCrossProduct(varVec1 As Variant, varVec2 As Variant) As =
Variant
Dim dblCross(2) As Double
dblCross(0) =3D varVec1(1) * varVec2(2) - varVec1(2) * varVec2(1)
dblCross(1) =3D varVec1(2) * varVec2(0) - varVec1(0) * varVec2(2)
dblCross(2) =3D varVec1(0) * varVec2(1) - varVec1(1) * varVec2(0)
GetCrossProduct =3D dblCross
End Function
Function VectorsAreEqual(varVec1 As Variant, varVec2 As Variant) As =
Boolean
Dim dblMag As Double
Dim dblDot As Double
Dim dblUnit1(2) As Double
Dim dblUnit2(2) As Double
dblMag =3D (varVec1(0) * varVec1(0) + varVec1(1) * varVec1(1) + =
varVec1(2) * varVec1(2)) ^ 0.5
dblUnit1(0) =3D varVec1(0) / dblMag: dblUnit1(1) =3D varVec1(1) / =
dblMag: dblUnit1(2) =3D varVec1(2) / dblMag
dblMag =3D (varVec2(0) * varVec2(0) + varVec2(1) * varVec2(1) + =
varVec2(2) * varVec2(2)) ^ 0.5
dblUnit2(0) =3D varVec2(0) / dblMag: dblUnit2(1) =3D varVec2(1) / =
dblMag: dblUnit2(2) =3D varVec2(2) / dblMag
dblDot =3D dblUnit1(0) * dblUnit2(0) + dblUnit1(1) * dblUnit2(1) + =
dblUnit1(2) * dblUnit2(2)
dblDot =3D Abs(dblDot - 1#)
' Compare within a tolerance
If dblDot < 0.0000000001 Then '1.0e-10
VectorsAreEqual =3D True
Else
VectorsAreEqual =3D False
End If
End Function
Sub SelectHoleEdges(swFace As SldWorks.Face2)
Dim swThisLoop As SldWorks.Loop2
Dim swThisCoEdge As SldWorks.CoEdge
Dim swPartnerFace As SldWorks.Face2
Dim swPartnerLoop As SldWorks.Loop2
Dim swPartnerCoEdge As SldWorks.CoEdge
Dim swEntity As SldWorks.Entity
Dim varThisNormal As Variant
Dim varPartnerNormal As Variant
Dim varCrossProduct As Variant
Dim varTangent As Variant
Dim vEdgeArr As Variant
Dim swEdge As SldWorks.Edge
Dim swCurve As SldWorks.Curve
Dim bRet As Boolean
Dim edgeParams As Variant
Set swThisLoop =3D swFace.GetFirstLoop
Do While Not swThisLoop Is Nothing
' Hole is inner loop
' Circular or elliptical hole has only one edge
If swThisLoop.IsOuter =3D False And 1 =3D =
swThisLoop.GetEdgeCount Then
Set swThisCoEdge =3D swThisLoop.GetFirstCoEdge
Set swPartnerCoEdge =3D swThisCoEdge.GetPartner
varThisNormal =3D GetFaceNormalAtMidCoEdge(swThisCoEdge)
varPartnerNormal =3D =
GetFaceNormalAtMidCoEdge(swPartnerCoEdge)
If Not VectorsAreEqual(varThisNormal, varPartnerNormal) Then
' There is a sufficient change between the two faces to =
determine
' what kind of transition is being made
varCrossProduct =3D GetCrossProduct(varThisNormal, =
varPartnerNormal)
varTangent =3D GetTangentAtMidCoEdge(swThisCoEdge)
If VectorsAreEqual(varCrossProduct, varTangent) Then
' hole
vEdgeArr =3D swThisLoop.GetEdges
Debug.Assert 0 =3D UBound(vEdgeArr)
Set swEdge =3D vEdgeArr(0)
Set swCurve =3D swEdge.GetCurve
' Ignore elliptical holes
If swCurve.IsCircle Then
Set swEntity =3D swEdge
'bRet =3D swEntity.Select4(True, swSelData)
edgeParams =3D swCurve.CircleParams
' The return value is the following array of 7 =
double values:
' [ center.x, center.y, center.z, axis.x, =
' Center and radius values are in meters.
Debug.Print "Center X,Y,Z =3D " + =
Str(edgeParams(0)); "," _
; Str(edgeParams(1)); ","; =
Str(edgeParams(2))
Debug.Print "Axis X,Y,Z =3D " + =
Str(edgeParams(3)); "," _
; Str(edgeParams(4)); ","; =
Str(edgeParams(5))
'Stop
'Debug.Assert bRet
End If
End If
End If
End If
' Move on to the next
Set swThisLoop =3D swThisLoop.GetNext
Loop
End Sub
Sub ALE_GetPanelInfo()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swBody As SldWorks.Body2
Dim swFace As SldWorks.Face2
Dim edgeList As Variant
Dim edgeCount As Long
Dim i As Long
Dim edgeObj As Object ' Edge object
Dim startVertexObj As Object ' Vertex objects
Dim endVertexObj As Object
Dim startPt As Variant ' Edge startpoint and endpoint arrays
Dim endPt As Variant
Set swApp =3D CreateObject("SldWorks.Application")
Set swModel =3D swApp.ActiveDoc
Set swPart =3D swModel
Set swBody =3D swPart.Body
Set swFace =3D swBody.GetFirstFace
Do While Not swFace Is Nothing
edgeCount =3D swFace.GetEdgeCount
Debug.Print "Number of edges on this face: " & edgeCount
edgeList =3D swFace.GetEdges ' Get the edges on the selected =
face
For i =3D 0 To (edgeCount - 1)
Set edgeObj =3D edgeList(i)
Set startVertexObj =3D edgeObj.GetStartVertex ' Get the =
"Start" vertex
Set endVertexObj =3D edgeObj.GetEndVertex ' Get the "End" =
vertex
If (Not startVertexObj Is Nothing) Then
startPt =3D startVertexObj.GetPoint ' Get the xyz =
vertex location
Debug.Print "Edge: " + Str\$(i) + " StartP " + =
Str\$(startPt(0)) + _
"," + Str\$(startPt(1)) + "," + =
Str\$(startPt(2))
End If
If (Not endVertexObj Is Nothing) Then
endPt =3D endVertexObj.GetPoint ' Get the xyz vertex =
location
Debug.Print "Edge: " + Str\$(i) + " EndP " + =
Str\$(endPt(0)) + _
"," + Str\$(endPt(1)) + "," + Str\$(endPt(2))
End If
Next i
SelectHoleEdges swFace
Set swFace =3D swFace.GetNextFace
Loop
End Sub