cancel
Showing results for 
Search instead for 
Did you mean: 

How to create new ‘NXOpen.Part’ if default PAX file is modified on local machine

Issue: When default PAX file is modified, I am getting error from NX as follows in log file,

Caught exception while running: Main

  1. NXException: The selected template doesn't exist

   at NXOpen.Builder.Commit()

 

I am using following code to create a new Part workspace (if not already available) in NX.

 

NXOpen.Part workPart = null;
workPart = Session.GetSession().Parts.Work;

if(workPart == null)
{
    FileNew fileNew = Session.GetSession().Parts.FileNew();

    fileNew.TemplateFileName = "model-plain-1-mm-template.prt";
    //fileNew.Application = FileNewApplication.Modeling; //Use for NX8.5 Deprecated 'Application & FileNewApplication' in NX9.0
    fileNew.ApplicationName = "ModelTemplate"; //For NX9.0
    fileNew.Units = Part.Units.Millimeters;
    fileNew.TemplateType = FileNewTemplateType.Item;

    //Here we invoke Save file dialog to get the file path from user
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    //Set filter options and filter index.
    saveFileDialog.Filter = "Part Files (*.prt)|*.prt";
    saveFileDialog.FilterIndex = 1;
    saveFileDialog.Title = "New Part";
    saveFileDialog.FileName = partFileName;
    //Call the ShowDialog method to show the dialog box.
    DialogResult userClickedSave = saveFileDialog.ShowDialog();
    //Process input if the user clicked OK.
    if (userClickedSave == DialogResult.OK)
    {
        partFileName = saveFileDialog.FileName;
        if (System.IO.File.Exists(partFileName))
            System.IO.File.Delete(partFileName);                
    }
    else
        return workPart;
 
    fileNew.NewFileName = partFileName;
    fileNew.Commit();
    fileNew.Destroy();
            
    //get valid "workPart" pointer
	workPart = Session.GetSession().Parts.Work;
}

 

This was working perfectly fine with all default settings. But one user modified the default PAX file as follows. The user wanted to have his own default Model Template to be used.

 

Please refer following attached image “PAX file modified by user.jpg”.

 

But now as default PAX file is modified, I am getting error from NX as follows in log file,

 

Caught exception while running: Main

  1. NXException: The selected template doesn't exist

   at NXOpen.Builder.Commit()

 

I observed that NX gives Error as ‘NXOpen.NXException: The selected template doesn't exist’ in following scenarios,

  • Even if "model-plain-1-mm-template.prt" is still available at the default installed location.
  • Even If I give correct full file path to the available template as follows,

 

string rootDir=Environment.GetEnvironmentVariable("UGII_ROOT_DIR");

fileNew.TemplateFileName = rootDir + "templates\\model-plain-1-mm-template.prt";
 

I have following queries,

  • How to select the Model Template path set by user in PAX files and use it in FileNew.
  • If above option is not possible then how to set complete template file path to “fileNew.TemplateFileName”?

 

Thanking you in advance.

5 REPLIES

Re: How to create new ‘NXOpen.Part’ if default PAX file is modified on local machine

Esteemed Contributor
Esteemed Contributor

A couple things I'd look at:

1) Spaces in filename/path causing an issue?

2) I'd echo the string "TemplateFileName" to the info window to make sure there are slashes at the end of "rootDir" (after line:

fileNew.TemplateFileName = rootDir + "templates\\model-plain-1-mm-template.prt";

)

3) After echo, make sure file is there (open DOS command prompt window, dir a "Dir ..cut & paste file spec from Info window..."

 

Ken

Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP5 + patch/TC11.2
I'd rather be e-steemed than e-diseaseled


Re: How to create new ‘NXOpen.Part’ if default PAX file is modified on local machine

Siemens Phenom Siemens Phenom
Siemens Phenom

 

Does it work interactively when you put paths in the pax file?  I would guess it does not.

 

I just talked to one of our CAD experts, and he said that you cannot use a path in the pax file to define template parts.  You can put all the template files you want into your templates directory, and refer to them in a customized pax file, but the pax file and the template part files need to be in the same folder.

 

If you do not want to use the ...ugii\templates folder, you can define the variable UGII_TEMPLATE_DIR to point to the folder that contains your pax files and template parts.

 

Regards,

 

Steve

 

Re: How to create new ‘NXOpen.Part’ if default PAX file is modified on local machine

Hi Steve,

 

Thank you for your reply.

I have reconfirmed that I can change the PAX file (this file location and name is not modified. It si same in default folder) and it still NX9.0 works.

 

I have updated PAX file to use My modified template file from my temporary folder on my local machine as follows.

Refer attached complete PAX file (I have modified extension from PAX to TXT for uploading) and Screen shot showing the use of modified template.

 

Please let me know How to pass complete file path of template file in the FileNew.TemplateFileName as follows,

Viz. Code snippet

FileNew fileNew = Session.GetSession().Parts.FileNew();

fileNew.TemplateFileName = "C:/model-plain-1-mm-template.prt";

 I am always getting error in log as follows if I try to use complete file path of template file ,

 

Caught exception while running: Main
NXOpen.NXException: The selected template doesn't exist
   at NXOpen.Builder.Commit()

 

I don't want to change UGII_TEMPLATE_DIR for all users. I just want to use my own template while creating New part file from my plugin using Parts.FileNew API. I will keep this template in my plugin's installation folder. I have to use this approach as some users might have modified the templates or PAX file on their local machines.

 

Please let me know how to pass complete file path of template file in the FileNew.TemplateFileName?

 

 

Thanking you for your help.

-Mahesh

Re: How to create new ‘NXOpen.Part’ if default PAX file is modified on local machine

Siemens Phenom Siemens Phenom
Siemens Phenom

Mahesh,

 

Good morning.  As far as I know, you cannot specify a path to a template file, either in your pax file like this in this clip from your attachment, or in the program.  You CAN however put the template file in the same folder as your pax file, and then refer to it by name, without the path, both in your pax file, and in the code.

 

You do not have to change the environment variable for all users - you can just change it for yourself.

 

Regards,

 

Steve

 

 <Filename>E:\users\MaheshR\model-plain-1-mm-template.prt</Filename>

Re: How to create new ‘NXOpen.Part’ if default PAX file is modified on local machine

Honored Contributor
Honored Contributor

An alternative to using the "FileNew" Builder would be to open your "template" part and perform a "save-as" to the file name and location specified. Or, use the OS file handling commands to copy and rename your template part then open the newly created copy.

 

In this way, you would not have to modify the pax files.