×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Auto title block from file path

Auto title block from file path

Auto title block from file path

(OP)
Hi!


I would like to extract information from file path into my drawings using title blocks. It seems to me in an other thread (http://www.eng-tips.com/viewthread.cfm?qid=364026) 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.

RE: Auto title block from file path

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

RE: Auto title block from file path

(OP)
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:
d:\3452.ProjectDescription\techno\45321.PartDescription\45321_PartRevision_PartDescription.prt

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.

RE: Auto title block from file path

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

RE: Auto title block from file path

(OP)
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 --> VB

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 

RE: Auto title block from file path

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

RE: Auto title block from file path

(OP)
I think I just did that. I recorded a journal creating a string type expression.

CODE --> VB

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")

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

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 --> VB

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. (:

RE: Auto title block from file path

Let's revisit the requirements: do you want to create an attribute or an expression?

www.nxjournaling.com

RE: Auto title block from file path

(OP)
: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:
http://www.nxjournaling.com/content/expressions-cr...

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

CODE --> VB

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.

RE: Auto title block from file path

Excellent!
Glad that you got it working.

www.nxjournaling.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources