' NX Add Associative callout by manual mode
' Journal created by Alto on 10-06-2015
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Annotations
Imports System.Windows.Forms
Imports NXOpen.UF
Imports NXOpen.Annotations.Annotation
Imports NXOpen.Assemblies
Module NXJournal
Dim theUI As UI = UI.GetUI
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession
Sub Main()
Call Mysub1()
Call Mysub2()
End Sub
Sub Mysub1()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, undoMarkName)
Dim myComponent As Assemblies.Component = Nothing
If SelectComponent("Select component", myComponent) = Selection.Response.Cancel Then
Return
End If
'$$$ specify attribute title to get from component
Const myAttrTitle As String = "Tool_ID"
Dim myAttrValue As String
Dim output As String
Try
myAttrValue = myComponent.GetStringAttribute(myAttrTitle)
output = "<W!" & myComponent.Tag.ToString & "@" & myAttrTitle & ">"
Clipboard.SetText(output)
Catch ex As NXException
If ex.ErrorCode = 512008 Then
'attribute not found
MessageBox.Show("Attribute '" & myAttrTitle & "' not found, journal exiting", "Attribute not found", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
Else
theSession.UndoToMark(markId1, undoMarkName)
MessageBox.Show(ex.Message, "Error: " & ex.ErrorCode, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Finally
End Try
lw.Close()
End Sub
Sub Mysub2()
'Dim myEdge As Object
Dim myPoint As Point3d
Dim myPointBalloon As Point3d
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim theUI As UI = UI.GetUI()
' Dim response As Selection.DialogResponse
Dim nullAnnotations_IdSymbol As Annotations.IdSymbol = Nothing
Dim idSymbolBuilder1 As Annotations.IdSymbolBuilder
Dim leaderData1 As Annotations.LeaderData
idSymbolBuilder1 = workPart.Annotations.IdSymbols.CreateIdSymbolBuilder(nullAnnotations_IdSymbol)
idSymbolBuilder1.Type = Annotations.IdSymbolBuilder.SymbolTypes.Circle
idSymbolBuilder1.Origin.Plane.PlaneMethod = Annotations.PlaneBuilder.PlaneMethodType.XyPlane
idSymbolBuilder1.UpperText = Clipboard.GetText
idSymbolBuilder1.Size = 0.35
leaderData1 = workPart.Annotations.CreateLeaderData()
leaderData1.StubSize = 0.25
leaderData1.Arrowhead = Annotations.LeaderData.ArrowheadType.FilledArrow
idSymbolBuilder1.Leader.Leaders.Append(leaderData1)
leaderData1.StubSide = Annotations.LeaderSide.Inferred
idSymbolBuilder1.Origin.SetInferRelativeToGeometry(True)
Dim myedge As object = Nothing
If UserSelectEdge("Select edge to attach balloon", myedge, myPoint) = Selection.Response.Cancel Then
Return
End If
'MsgBox(myPoint.ToString())
Dim nullview As NXOpen.View = Nothing
Dim point1_1 As Point3d = New Point3d(myPoint.X, myPoint.Y, 0)
Dim point2_1 As Point = workPart.Points.CreatePoint(point1_1)
Dim point3_1 As Point3d = New Point3d(myPoint.X, myPoint.Y, 0.0)
leaderData1.Leader.SetValue(point2_1, workPart.Views.WorkView, point3_1)
Dim assocOrigin1 As Annotations.Annotation.AssociativeOriginData = Nothing
assocOrigin1.View = nullview
assocOrigin1.ViewOfGeometry = nullview
assocOrigin1.XOffsetFactor = 0.0
assocOrigin1.YOffsetFactor = 0.0
idSymbolBuilder1.Origin.SetAssociativeOrigin(assocOrigin1)
Dim response2 As Selection.DialogResponse = UserSelectScreenPos("Place balloon", myPointBalloon)
If response2 <> Selection.DialogResponse.Pick Then
Return
End If
Dim point4_1 As Point3d = New Point3d(myPointBalloon.X, myPointBalloon.Y, 0.0)
idSymbolBuilder1.Origin.Origin.SetValue(Nothing, nullview, point4_1)
Dim nXObject1 As NXObject
nXObject1 = idSymbolBuilder1.Commit()
idSymbolBuilder1.Destroy()
End Sub
Function UserSelectEdge(ByVal prompt As String, ByRef selObj As TaggedObject, ByRef selPoint As Point3d) As Selection.Response
'Allow user to interactively select an edge
Dim title As String = "Select an edge"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(6) As Selection.MaskTriple
'Set the selection criteria to any edge
'TODO: Add point on surface
selectionMask_array(0).Type = UFConstants.UF_solid_type
selectionMask_array(0).Subtype = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE
selectionMask_array(0).SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE
selectionMask_array(1).Type = UFConstants.UF_line_type
selectionMask_array(1).Subtype = UFConstants.UF_all_subtype
selectionMask_array(2).Type = UFConstants.UF_circle_type
selectionMask_array(2).Subtype = UFConstants.UF_all_subtype
selectionMask_array(3).Type = UFConstants.UF_conic_type
selectionMask_array(3).Subtype = UFConstants.UF_all_subtype
selectionMask_array(4).Type = UFConstants.UF_spline_type
selectionMask_array(4).Subtype = UFConstants.UF_all_subtype
selectionMask_array(5).Type = UFConstants.UF_solid_silhouette_type
selectionMask_array(5).Subtype = UFConstants.UF_all_subtype
selectionMask_array(6).Type = UFConstants.UF_section_edge_type
selectionMask_array(6).Subtype = UFConstants.UF_all_subtype
'This line allows the user to select from any view:
ufs.Ui.SetCursorView(0)
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selObj, selPoint)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
Function UserSelectScreenPos(ByVal prompt As String, ByRef selPoint As Point3d) As Selection.DialogResponse
'Allow user to interactively select a screen position
Dim view As NXOpen.View = Nothing
Return theUI.SelectionManager.SelectScreenPosition(prompt, view, selPoint)
End Function
Function SelectComponent(ByVal prompt As String, ByRef selObj As NXObject) As Selection.Response
Dim title As String = "Select a component"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_all_subtype
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selObj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------
End Function
End Module