Quantcast
Channel: NX Design Forum topics
Viewing all articles
Browse latest Browse all 5175

Open UGPART(non master) from Master part and automatically switch to drafting solution

$
0
0

Hello guys,

 

I have found a code on a forum for an older NX version wich opens the non master part for the selected component in assembly navigator.

I have adapted it to switch automatically to drafting after opening the drawing using the new NX 11.00 function.

The script works by searching all UGPART wich have the same name as the UGMASTER but with the extension added in customer defaults(in my case it can be -DWG1 or -dwg1 or 2).

To use it you have to compile it as .dll or you can save it as .vb.

 


Option Strict Off

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpen.UI


Module NXJournal

Dim theSession As Session = Session.GetSession()
Dim workPart = theSession.Parts.Work
Dim theUFS As NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession()
Dim theUI As UI = ui.GetUI
Dim curSession As NXOpen.Session = NXOpen.Session.GetSession()
'Dim lw As ListingWindow = theSession.ListingWindow
Dim lg As LogFile = theSession.LogFile

Dim SelectedList As New List(Of String)
Dim bolSelected As Boolean = False

Dim objSelected As NXObject
Dim intType As Integer
Dim intSubType As Integer
Dim ExtensionList As New List(Of String)
Dim blnFoundSpec As Boolean = False


Sub Main()
'lw.Open()
lg.WriteLine("~~ Journal: Open_Specification.vb ~~")
lg.WriteLine(" timestamp: " & Now)

BuildExtensionList()

Try
lg.WriteLine(" Find out if components are selected.")
AreComponentsSelected()
If bolSelected = False Then
lg.WriteLine(" No components are selected.")
lg.WriteLine(" Open drawing for current work part.")
OpenDrawings(workPart.GetStringAttribute("DB_PART_NO") & "/" & workPart.GetStringAttribute("DB_PART_REV"))
Else
For Each selectedcomponent As String in SelectedList
lg.WriteLine(" Components are selected.")
lg.WriteLine(" Open drawing for selected parts.")
OpenDrawings(selectedcomponent)
Next
End If
If blnFoundSpec Then
curSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")
Else
MessageBox.Show("You shall not pass!(NO UGPART found)", "Gates of Mordor", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If

Catch ex As Exception
lg.WriteLine(" Error in Sub Main: " & ex.Message)
End Try
'lw.Close()
lg.WriteLine("~~ Journal: Open_Specification.vb completed ~~")
lg.WriteLine(" timestamp: " & Now)
End Sub

Sub AreComponentsSelected()
Try
Dim intNumSelected As Integer = theUI.SelectionManager.GetNumSelectedObjects()
If intNumSelected = 0 Then
bolSelected = False
Exit Sub
End If

For i As Integer = 0 To intNumSelected-1
objSelected = theUI.SelectionManager.GetSelectedObject(i)
theUFS.Obj.AskTypeAndSubtype(objSelected.Tag, intType, intSubType)
If intType = UFConstants.UF_component_type Then
Dim theComp As Component = DirectCast(objSelected, Component)
SelectedList.Add(theComp.DisplayName)
bolSelected = True
End If
Next
Catch ex As Exception
lg.WriteLine(" Error in Sub AreComponentsSelected: " & ex.Message)
End Try

End Sub

Sub BuildExtensionList()
'Add new extensions to this list.
'This list should be sorted by most commonly used extension as opening will be attempted in this order.
ExtensionList.Add("-DWG1")
ExtensionList.Add("-dwg1")
ExtensionList.Add("-dwg2")
ExtensionList.Add("-DWG2")

End Sub

Sub OpenDrawings(ByVal OpenMe As String)
Dim strExtension As String
Dim strSplitString() As String = Split(OpenMe, "/")
Dim strPartNo As String = strSplitString(0)
Dim strRevNum As String = strSplitString(1)
Dim strOpenString As String = ""

For Each strExtension In ExtensionList
Try
strOpenString = "@DB/" & strPartNo & "/" & strRevNum & "/specification/" & strPartNo & "-" & strRevNum & strExtension

Try
theSession.Parts.SetNonmasterSeedPartData(strOpenString)
Dim prtBasePart As BasePart
Dim lsBasePart As PartLoadStatus
prtBasePart = theSession.Parts.OpenBaseDisplay(strOpenString, lsBasePart)
lsBasePart.Dispose()
blnFoundSpec = True
Exit For

Catch exc As Exception
Dim prtPart As Part = CType(theSession.Parts.FindObject(strOpenString), Part)
Dim lsPart As PartLoadStatus
Dim status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(prtPart, False, True, lsPart)
lsPart.Dispose()
blnFoundSpec = True
Exit For

End Try

Catch ex As Exception
lg.WriteLine(" Error in Sub OpenDrawings:")
lg.WriteLine(" " & ex.Message & ": " & strOpenString)
End Try
Next
End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY
End Function
End Module

 

 


Viewing all articles
Browse latest Browse all 5175

Trending Articles