Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto title block from file path

Status
Not open for further replies.

engtipsuser100

Mechanical
Jun 14, 2014
13
Hi!


I would like to extract information from file path into my drawings using title blocks. It seems to me in an other thread ( this problem was already discussed, and even two journals were written to solve it. I did run both journals but for some reason I could not find the attributes that these journals was supposed to create.

What am I doing worng? Could you please help me out? I even tried to create the same directory tree to make the journals work.


Thanks in advance! :)


P.s.: sorry for creating a new thread but I was not able to post in the old one.
 
Replies continue below

Recommended for you

Be careful with the code in the other thread; it reads the path information of the display part and writes attributes to the work part. The display part and the work part are not necessarily one and the same.

Can you give an example of the file path and what information you want to pull out of it? It does not have to be a real file path, it can be "fabricated" to keep your information private, as long as the overall structure is representative of the real deal. Also, what version of NX are you using?

www.nxjournaling.com
 
Thanks for the reply! I am not sure if I can tell the difference between display and work part. It has to do something with assemblies?

Yes, I can. We have a dedicated drive for our projects which looks like this:
[tt]d:\3452.ProjectDescription\techno\45321.PartDescription\45321_PartRevision_PartDescription.prt[/tt]

In the example above 3452 is the project ID and 45321 is the part ID. I was able to get the part ID using the ug_askCurrentWorkPart() and substring() functions. Now I would like to make an automatic title block for the project ID too, but I was not able to figure it out.

We use NX6.
 
The code below illustrates some string manipulation techniques; basically it just breaks the string into parts and extracts the parts at certain locations. It assumes that the string in question will always be of the form: "{drive letter}:\{project number}.{project description}\{other stuff}". If the format is consistent, this code should work; if/when the format changes, the code will need updating.

Rather than using a hard coded string value, you would be working with the part.FullPath property.

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim filePath As String = "D:\3452.ProjectDescription\techno\45321.PartDescription\45321_PartRevision_PartDescription.prt"

        'split the file path into parts
        Dim pathParts() As String = filePath.Split("\")

        'split the project directory name
        Dim projectParts() As String = pathParts(1).Split(".")

        'lw.WriteLine("filePath: " & filePath)

        'For Each temp As String In pathParts
        '    lw.WriteLine(temp)
        'Next

        lw.WriteLine("project number: " & projectParts(0))
        lw.WriteLine("project description: " & projectParts(1))

        lw.Close()

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module



www.nxjournaling.com
 
Thanks for the guidance! :)

I was able to use the FullPath property and I also realized we use underscore instead of dot to separate project ID from project description, so I modified the code to split at underscores. But I still could not find a way to create an attribute from the project ID or the filepath. I tried to record my own expression creation to work it out but I got stuck there.

Could you please give me some advice how to do it?

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim filePath As String = theSession.Parts.Work.FullPath

        'split the file path into parts
        Dim pathParts() As String = filePath.Split("\")

        'split the project directory name
        Dim projectParts() As String = pathParts(1).Split("_")

	Dim expression999 As Expression
	expression999 = workPart.Expressions.CreateExpression("String", "filepath999=filePath")

        lw.WriteLine("filePath: " & filePath)

        'For Each temp As String In pathParts
        '    lw.WriteLine(temp)
        'Next

        lw.WriteLine("project number: " & projectParts(0))
        lw.WriteLine("project description: " & projectParts(1))

        lw.Close()

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module

 
I'd suggest recording a journal while creating a string attribute on your part file. Edit the resulting code to your desired attribute title and instead of a "hard coded" attribute value, use the desired "project" value from the string manipulations.

www.nxjournaling.com
 
I think I just did that. I recorded a journal creating a string type expression.

Code:
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
'   Menu: Tools->Expression...
' ----------------------------------------------
' ----------------------------------------------
'   Dialog Begin Insert Function
' ----------------------------------------------
' ----------------------------------------------
'   Dialog Begin Function Arguments
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

[b]Dim expression1 As Expression
expression1 = workPart.Expressions.CreateExpression("String", "test=dateTimeString()")[/b]

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Update Expression Data")

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

theSession.DeleteUndoMark(markId2, "Update Expression Data")

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

Then I copied the relevant lines, modified them a little bit to point to projectPart() and inserted to the "main" journal.

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim filePath As String = theSession.Parts.Work.FullPath

        'split the file path into parts
        Dim pathParts() As String = filePath.Split("\")

        'split the project directory name
        Dim projectParts() As String = pathParts(1).Split("_")

		Dim expression1 As Expression
		expression1 = workPart.Expressions.CreateExpression("String", "test2=projectParts(0)")

        lw.WriteLine("filePath: " & filePath)

        'For Each temp As String In pathParts
        '    lw.WriteLine(temp)
        'Next

        lw.WriteLine("project number: " & projectParts(0))
        lw.WriteLine("project description: " & projectParts(1))

        lw.Close()

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module

I ran the journal, and I came up with a syntax error. I don't know what I am doing wrong and sorry if I'm testing your patience with my thick head. :)
 
:D Yes, meanwhile I figured out too that's an important question. Now I know, I definitely want an expression. But never mind, I was able to find a solution.

I found a great article about creating expressions here:

I had to modify the syntax a little bit. Now here is the working code:

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim filePath As String = theSession.Parts.Work.FullPath

        'split the file path into parts
        Dim pathParts() As String = filePath.Split("\")

        'split the project directory name
        Dim projectParts() As String = pathParts(1).Split("_")

Dim ProjectNumber As Expression = Nothing
Dim ProjectNumberValue As String = projectParts(0)
ProjectNumber = workPart.Expressions.CreateExpression("String","ProjectNumber=" & """" & ProjectNumberValue & """")

        lw.WriteLine("filePath: " & filePath)

        'For Each temp As String In pathParts
        '    lw.WriteLine(temp)
        'Next

        lw.WriteLine("project number: " & projectParts(0))
        lw.WriteLine("project description: " & projectParts(1))

        lw.Close()

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module

And it works! This is amazing! Thank you for your help. :)

The next thing is going be the error handling and autorun.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor