Hi,
I try to automatically create sketch elements like notes, hatches and lines when creating a view based on a part in a drawing. The sketch elements must be created when attributes defined on some faces of the part.
The problem is that SW crashes on the creation of the sketch elements.
Here is the code:
Public Class DrawingEventHandler
Inherits DocumentEventHandler
Dim WithEvents swDrawing As DrawingDoc
Overrides Function Init(ByVal sw As SldWorks, ByVal addin As LtpWorksAddin, ByVal model As ModelDoc2) As Boolean
swDrawing = model
End Function
Overrides Function AttachEventHandlers() As Boolean
AddHandler swDrawing.AddItemNotify, AddressOf Me.DrawingDocEvents_AddItemNotify
End Function
Function DrawingDocEvents_AddItemNotify(ByVal EntityType As Integer, ByVal itemName As String) As Integer
If (EntityType = swNotifyEntityType_e.swNotifyDrawingView) Then
Dim swView As SolidWorks.Interop.sldworks.View = GetDrawingView(swDrawing, itemName)
If swView IsNot Nothing Then
Dim swModel As ModelDoc2 = swView.ReferencedDocument
Dim swPart As PartDoc = swModel
Dim swAttributeDef As AttributeDef = swAddin.SwApp.DefineAttribute("MyAttribute")
swAttributeDef.AddParameter("TextAttribute", swParamType_e.swParamTypeString, 0, 0)
swAttributeDef.Register()
For Each swBody As Body2 In swPart.GetBodies2(swBodyType_e.swAllBodies, True)
For Each swEntFace As Entity In swBody.GetFaces()
Dim swAtt As SolidWorks.Interop.sldworks.Attribute = swEntFace.FindAttribute(swAttributeDef, 0)
If swAtt IsNot Nothing Then
Dim swParamText As Parameter = swAtt.GetParameter("TextAttribute")
If swParamText IsNot Nothing Then Debug.Print("FindAttribute " & swParamText.GetStringValue)
Dim swDrawingDoc As ModelDoc2 = swDrawing
Dim swSketchMgr As SketchManager
Dim swSketchSegment As SketchSegment
swSketchMgr = swDrawingDoc.SketchManager
swDrawing.ActivateView(itemName)
' The following line crashes
If swDrawing.ActiveDrawingView IsNot Nothing Then swSketchSegment = swSketchMgr.CreateLine(0, 0, 0, 0.1, 0, 0)
End If
Next
Next
End If
End If
End Function
End Class
SolidworksApi macros