×
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

journal IO.file.delete

journal IO.file.delete

journal IO.file.delete

(OP)
I am having trouble using a IO.File.Delete command within NX's journal. The error message I keep receiving is attached. This is a part of a bigger program that creates a "_rev" version of my current workpart. There is an intermediate part "delete_rev”, which I need to manually go through and delete files but I would like to automate this. The following snippet is what I plan to add to the code but for some reason cannot delete the "delete_rev" part from NX. I can however delete the "_rev" and original part but that doesn't help me automate this.

Additional snippet:

CODE --> vb

Module NXJournals
Sub Main
Dim theUI As UI = UI.GetUI()
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession() 
Dim workpart As Part = theSession.Parts.work
Dim Revcomp As String
        Revcomp = path.GetFileNameWithoutExtension(workPart.FullPath)
        Dim folderName As String = Path.GetDirectoryName(workpart.FullPath)
        Dim fullpathname2 As String = folderName & "\" & "delete_" & Revcomp & ".prt"

'delete
If IO.File.Exists(fullpathname2) Then
MsgBox("Deleting.")
 IO.File.Delete(fullpathname2)
                End If

End Sub
End Module 

Original Code

CODE --> vb

Option Strict Off  
Imports System 
Imports System.Windows.Forms 
Imports System.IO  
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Threading
Imports NXOpen  
Imports NXOpen.UF 
Imports NXOpen.Assemblies

Module NXJournal
Sub Main

Dim theUI As UI = UI.GetUI()
Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim partname As String  
Dim workpart As Part = theSession.Parts.work
Dim disp_partname As String
Dim displayPart As Part = theSession.Parts.Display
Dim comp As String
Dim Revcomp As String
Dim insert as String
Dim partLoadStatus2 As NXOpen.PartLoadStatus = Nothing
 partname = path.GetFileNameWithoutExtension(workPart.FullPath)

comp = partname
         insert = "__Prep"
        'Revcomp = comp .insert(5, insert)
Dim dIndex = comp.IndexOf("_")
Revcomp=comp.insert(dIndex, insert)

'To find folder name and such
        Dim folderName As String = Path.GetDirectoryName(workpart.FullPath)
        Dim fullpathname1 As String = "C:\Program Files\UGS\NX 7.5\UGII\html_files/model_template_inch.prt"
        Dim fullpathname2 As String = folderName & "\" & "delete_" & Revcomp
        
If File.Exists(fullpathname2 & ".prt") Then 
If Msgbox("File Already Exists. Do you want to overwrite?", vbyesno + vbquestion, "File Already Exists") = vbno then
Exit Sub
Else
fullpathname2 = fullpathname2 & "_"
End If
End If

'Msgbox(comp & chr(13) & Revcomp)

            Dim partLoadStatus3 As PartLoadStatus
            Dim status1 As PartCollection.SdpsStatus
            status1 = theSession.Parts.SetDisplay(displaypart, False, True, partLoadStatus3)
            workPart = theSession.Parts.Work
            displayPart = theSession.Parts.Display
            partLoadStatus3.Dispose()
            'Msgbox(partname)

Fullpathname2 = fullpathname2 & ".prt"
        File.Copy(fullpathname1, fullpathname2)

        Dim basePart1 As BasePart = theSession.Parts.OpenBase(fullpathname2, partLoadStatus2)
        Dim part2 As Part = CType(basePart1, Part)
        Dim basePoint1 As Point3d = New Point3d(0.0, 0.0, 0.0)
        Dim orientation1 As Matrix3x3
        orientation1.Xx = 1.0
        orientation1.Xy = 0.0
        orientation1.Xz = 0.0
        orientation1.Yx = 0.0
        orientation1.Yy = 1.0
        orientation1.Yz = 0.0
        orientation1.Zx = 0.0
        orientation1.Zy = 0.0
        orientation1.Zz = 1.0
        Dim component1 As Component = workpart.ComponentAssembly.AddComponent(part2, "Model", Revcomp, _
                                                         basePoint1, orientation1, 200, partLoadStatus2)

Dim partSaveStatus1 As PartSaveStatus
Dim partLoadStatus1 As PartLoadStatus
theSession.Parts.SetWorkComponent(component1, partLoadStatus1)
workPart = theSession.Parts.Work
partLoadStatus1.Dispose()
Dim fullpathname3 As string
fullpathname3=folderName & "\" & Revcomp & ".prt"

If File.Exists(fullpathname3) Then 
fullpathname3=foldername & "\" & Revcomp & "_.prt"
End If

'Msgbox(fullpathname2)
partSaveStatus1 = workPart.SaveAs(fullpathname3)
partSaveStatus1.Dispose()

        Dim mySolid As NXObject  
        If SelectSolid("Select a solid", mySolid) = Selection.Response.Cancel Then  
            Exit Sub  
        End If  

        Dim markId1 As Session.UndoMarkId  
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")  

        Dim nullFeatures_Feature As Features.Feature = Nothing  

        If Not workPart.Preferences.Modeling.GetHistoryMode Then  
            Throw (New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))  
        End If  

        Dim waveLinkBuilder1 As Features.WaveLinkBuilder  
        waveLinkBuilder1 = workPart.BaseFeatures.CreateWaveLinkBuilder(nullFeatures_Feature)  

        Dim extractFaceBuilder1 As Features.ExtractFaceBuilder  
        extractFaceBuilder1 = waveLinkBuilder1.ExtractFaceBuilder  

        extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain  

        waveLinkBuilder1.Type = Features.WaveLinkBuilder.Types.BodyLink  

        extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain  

        'waveLinkBuilder1.CopyThreads = False  

        extractFaceBuilder1.ParentPart = Features.ExtractFaceBuilder.ParentPartType.OtherPart  

        theSession.SetUndoMarkName(markId1, "WAVE Geometry Linker Dialog")  

        extractFaceBuilder1.Associative = True  

        extractFaceBuilder1.FixAtCurrentTimestamp = False  

        extractFaceBuilder1.HideOriginal = False  

        extractFaceBuilder1.InheritDisplayProperties = False  

        Dim selectObjectList1 As SelectObjectList  
        selectObjectList1 = extractFaceBuilder1.BodyToExtract  

        'extractFaceBuilder1.CopyThreads = False  

        Dim added1 As Boolean  
        added1 = selectObjectList1.Add(mySolid)  

        Dim nXObject1 As NXObject  
        nXObject1 = waveLinkBuilder1.Commit()  

        theSession.SetUndoMarkName(markId1, "WAVE Geometry Linker")  

        waveLinkBuilder1.Destroy()   
End Sub

   Function SelectSolid(ByVal prompt As String, ByRef selObj As NXObject) As Selection.Response  

        Dim theUI As UI = UI.GetUI  
        Dim title As String = "Select a solid"  
        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_solid_type  
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY  
        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 arg As String) As Integer
        Return CType(Session.LibraryUnloadOption.Immediately, Integer)
    End Function
End Module 

Denis Huskic
Data Prep
Kettering University
Class of '17

RE: journal IO.file.delete

The error message is telling you that you don't have privileges to delete the file. Either you do not have ownership of the file, it is in use by the system and "locked" (possible if your journal creates the file, does something with it and tries to delete it without closing it first), or the file or one of its parent folders is marked as "read only". It looks like you are copying a template part and renaming it for your purposes, so it is probably the ownership issue and/or the file is marked as read-only.

p.s.
It may not be a problem, but I don't recommend mixing forward and backward slashes when referencing a file path.

CODE

Dim fullpathname1 As String = "C:\Program Files\UGS\NX 7.5\UGII\html_files/model_template_inch.prt" 

www.nxjournaling.com

RE: journal IO.file.delete

(OP)
I agree 100% with that last note, quiet embarrassing on my end. You read the journal correctly, I copy a template, rename it to match the work part with a slight revision and then link over that body. Next, I attempted to delete an extra file that was created in the process, this is where I struggle. I tried to take into account the fact that the file could be locked. I read that creating a second module for the journal would help solve this, obviously this may not be true. Could you offer any insight on "unlocking" it? I am trying to move it to another directory and then attempt to delete it but that is proving to be a dead end as well.

Denis Huskic
Data Prep
Kettering University
Class of '17

RE: journal IO.file.delete

(OP)
I figured it out! It was locked because it was marked as a "read-only" file. I just put a small snippet into the code before I tried the delete command and it works just fine now. I'll be sure to keep this in mind for future journals. Here is the code, keep in mind I did some additional edditing so the path names might not match:

CODE --> vb

IO.File.SetAttributes(fullpathname3, IO.FileAttributes.Normal)
IO.File.Delete(fullpathname3) 

Denis Huskic
Data Prep
Kettering University
Class of '17

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