Cancel
Showing results for 
Search instead for 
Did you mean: 

Opening part files

Pioneer
Pioneer

Hey,

i have a problem in my application opening a part file.

Following code:

public void loadPrt(String PathofSession,String SessionName)        
        {
           if (theSession.Parts.ToArray().Length != 0)       //closes an opened part
           {
               theSession.Parts.Work.Close(NXOpen.BasePart.CloseWholeTree.False, NXOpen.BasePart.CloseModified.CloseModified, null);
           }               
           
           PartLoadStatus partloadstatus;                                 
           theSession.Parts.Open(PathofSession + SessionName, out partloadstatus);    //open new part

PathofSession is "c:\\NXToolPath\\" and SessionName is "Master.prt"

 

Unfortunately i get an exception as follows:

"An unhandled exception of type 'System.AccessViolationException' occurred in NXOpen.dll" at Open() Method - saying that tried to read/write in protected memory.

 

I am using .netframework 4.0 and Visual Studio 2012

 

Does anybody have had the same issue or an idea to solve it?

I tried different files and filepaths, doesn't help.

 

Thanks in advance!

16 REPLIES

Re: Opening part files

Honored Contributor
Honored Contributor

I've not seen this particular error, but you might get more information if you add some error handling; perhaps something like (VB used below):

 

        Dim fileName As String
        Try
            fileName = IO.Path.Combine(PathOfSession, SessionName)
            lw.WriteLine("filename: " & fileName)

        Catch ex As Exception
            lw.WriteLine("error: " & ex.Message)
            'other error handling code...
        End Try

        If IO.File.Exists(fileName) Then
            Try
                Dim partLoadStatus1 As PartLoadStatus
                theSession.Parts.Open(fileName, partLoadStatus1)

            Catch ex As Exception
                lw.WriteLine("error while opening file: " & ex.Message)
                'also check the value of 'partLoadStatus1'

            End Try
        Else
            'report file not found
            lw.WriteLine("file not found: '" & fileName & "'")
            'other error handling code...
        End If

 

Also check the release notes of your NX version, it will list which .net framework to use when programming NX.

Re: Opening part files

Experimenter
Experimenter

Error will occur If session doesn't have workpart and code trying to access the workPart property.

 

Add this code inside the if condition.

 

try
{    
Part part1 = (Part)theSession.Parts.FindObject(sessionName);
    part1.Close(NXOpen.BasePart.CloseWholeTree.True, NXOpen.BasePart.CloseModified.UseResponses, null);
}
catch
{
}
Selvaraj Chellamuthu
NX Automation Engineer
NX 9.0 | Teamceneter 10.1

Re: Opening part files

Pioneer
Pioneer

I have tried both, but still the same error. Smiley Sad

 

@cowski1: Unfortunately i don't get any further information via the try-catch operation. all i found out via exception details is that openbasedisplay() takes 3 arguments (NXOpen.PartCollection.JA_PART_open_base_display(IntPtr filename, Tag& part, IntPtr& loadStatus) ) ??

Where exactly in the release info do i find the .net framework?

 

@SelvarajC: There is no part opened so i won't get the workpart from theSession. Is there a way to directly access the workpart from fileName?

 

thanks

ingfu.

Re: Opening part files

Experimenter
Experimenter

There is no part opened so i won't get the workpart from theSession. Is there a way to directly access the workpart from fileName?

 

// Way to access the Work part

Part workPart = theSession,Parts.Work;

if(workPart == null)
{
theSession.ListingWindow.WriteLine("No work part found");
}

Above code shows the way of collecting the work part.

if "workPart" variable is null then session does not have any work part.

 

Do you want to collect a part using specified name?

 

 

Selvaraj Chellamuthu
NX Automation Engineer
NX 9.0 | Teamceneter 10.1

Re: Opening part files

Pioneer
Pioneer

theSession.Parts.work = null

beceause there is no part loaded yet.

I want to load components from the fileName (c:\\NXToolPath\\Master.prt)

Re: Opening part files

Experimenter
Experimenter
Do you want to load as work part?
Selvaraj Chellamuthu
NX Automation Engineer
NX 9.0 | Teamceneter 10.1

Re: Opening part files

Pioneer
Pioneer
            theUI = UI.GetUI();                                           
            theUfSession = UFSession.GetUFSession();                        
            workPart = theSession.Parts.Work;                              
            displayPart = theSession.Parts.Display;

This would be the next code.

Yes i want to load workpart.

Re: Opening part files

Experimenter
Experimenter
// Try this piece of code. 

NXOpen.Session theSession = NXOpen.Session.GetSession(); NXOpen.Part[] parts = theSession.Parts.ToArray(); string filePath = @"c:\\NXToolPath\\Master.prt"; NXOpen.Part expectedPart = null;
// Collect the part file if already opened foreach (var item in parts) { if (System.IO.Path.GetFileNameWithoutExtension(item.FullPath.ToUpperInvariant()) == System.IO.Path.GetFileNameWithoutExtension(filePath.ToUpperInvariant())) { expectedPart = item; } } if (expectedPart == null) { NXOpen.PartLoadStatus loadStatus; theSession.Parts.OpenBaseDisplay(filePath, out loadStatus); loadStatus.Dispose(); } else { NXOpen.PartLoadStatus loadStatus; theSession.Parts.SetDisplay(expectedPart, false, true, out loadStatus); loadStatus.Dispose(); }
Selvaraj Chellamuthu
NX Automation Engineer
NX 9.0 | Teamceneter 10.1

Re: Opening part files

Pioneer
Pioneer

Thank you for the quick answers.

UNfortunately the given code produces same error as before in

theSession.Parts.OpenBaseDisplay(filePath, out loadStatus);

 

I wonder if this error has something to do with the x64 application and the x86- NXOpen.dll