Cad Cam Development



Step by step NX Open C++ Introduction

  1. Open UGS NX6 and check you have C++ journal code generation
  2. Check you have C++ generation language selected

    Start recording journal and click on the actions you want to perform. There is a cylinder creation in my example.Start recording journal

  3. Once you have completed stop recording journal and check code. In case of cylinder the generated code should look like this:

  4. Session *theSession = Session::GetSession();
    Part *workPart(theSession->Parts()->Work());
    Part *displayPart(theSession->Parts()->Display());
    // ----------------------------------------------
    // Menu: Insert->Design Feature->Cylinder...
    // ----------------------------------------------
    workPart->FacetedBodies()->DeleteTemporaryFacesAndEdges();Session::UndoMarkId markId1;
    markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible, "Start");Features::Feature *nullFeatures_Feature(NULL);
    Features::CylinderBuilder *cylinderBuilder1;
    cylinderBuilder1 = workPart->Features()->CreateCylinderBuilder(nullFeatures_Feature);cylinderBuilder1->BooleanOption()->SetType(GeometricUtilities::BooleanOperation::BooleanTypeCreate);std::vector targetBodies1(1);
    Body *nullBody(NULL);
    targetBodies1[0] = nullBody;
    cylinderBuilder1->BooleanOption()->SetTargetBodies(targetBodies1);theSession->SetUndoMarkName(markId1, "Cylinder Dialog");Session::UndoMarkId markId2;
    markId2 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Start");theSession->SetUndoMarkName(markId2, "Vector Dialog");// ----------------------------------------------
    // Dialog Begin Vector
    // ----------------------------------------------
    Session::UndoMarkId markId3;
    markId3 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Vector");Point3d origin1(0.0, 0.0, 0.0);
    Vector3d vector1(0.0, 0.0, 1.0);
    Direction *direction1;
    direction1 = workPart->Directions()->CreateDirection(origin1, vector1, SmartObject::UpdateOptionWithinModeling);theSession->DeleteUndoMark(markId3, NULL);theSession->SetUndoMarkName(markId2, "Vector");theSession->DeleteUndoMark(markId2, NULL);Axis *axis1;
    axis1 = cylinderBuilder1->Axis();axis1->SetDirection(direction1);cylinderBuilder1->Diameter()->SetRightHandSide("50");cylinderBuilder1->Height()->SetRightHandSide("150");Session::UndoMarkId markId4;
    markId4 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Cylinder");NXObject *nXObject1;
    nXObject1 = cylinderBuilder1->Commit();

    theSession->DeleteUndoMark(markId4, NULL);

    theSession->SetUndoMarkName(markId1, "Cylinder");

    cylinderBuilder1->Destroy();

    workPart->FacetedBodies()->DeleteTemporaryFacesAndEdges();

  5. Now open Visual Studio, and create C++ Dll project with blank template.
  6. Create and add dllmain.cpp file to your source files. And add the following functions to it:

  7. UGSDLLTEST_API int ufusr_ask_unload()
    {
    return (int)Session::LibraryUnloadOptionImmediately;
    }
    UGSDLLTEST_API void ufusr(char * param,int * retcod,int param_len)
    {
    int i = UF_initialize();
    if(i!=0)
    MessageBox(NULL,L"initialize error",L"init error",MB_OK);UF_terminate();
    }where UGSDLLTEST_API :

    #ifdef UGSDLLTEST_EXPORTS
    #define UGSDLLTEST_API __declspec(dllexport)
    #else
    #define UGSDLLTEST_API __declspec(dllimport)
    #endif

    First function int ufusr_ask_unload() allows to  choose between different resource deallocation type. You can choose between LibraryUnloadOptionAtTermination, LibraryUnloadOptionExplicitly and LibraryUnloadOptionImmediately. If you don’t specify this function LibraryUnloadOptionAtTermination is set by default. This means UGS does not release exe or dll file until you close it. If you specify LibraryUnloadOptionImmediately UGS immediateley releases after use dll/exe file you’ve been working through NX Open invocation. This is how we want in NX Open application development.

    Second function void ufusr(char * param,int * retcod,int param_len) is specific NX Open function which you have to provide to have custom NX open application working in your NX environment. It doesn’t matter whether you’re creating dll or exe, this function is a start point for any NX Open unmanaged application. See more for NX Open documentation for more details.

  8. Add libufun.lib, libugopenint.lib, libnxopencpp.lib to linker properties so that application could be linked.
  9. VS linker properties

  10. Now add this code to your ufusr function and test it. A 150 mm height cylinder should appear. To do this click from menu file->execute->NxOpen, and ensure that a DLL file filter is set in the application type. If you don’t see execute menu, change your UGS type to advanced with full menus
  11. Additional improvements
  12. Play araound with code generated by journal and find unnecessary lines.

  13. Result
  14. C++ NX Open final result

Advertisements

Trackbacks & Pingbacks

Comments

  1. * Tarun Choudhary says:

    Thank you sir, for sharing such a precious knowledge.

    | Reply Posted 7 years ago
  2. * Tarun Choudhary says:

    i request you to share some more examples in CAM using NX Open C/C++ ……..

    | Reply Posted 7 years ago
    • * Grzegorz says:

      Ok. I’ll provide some, but please be patient, because I’m too busy for a moment.

      | Reply Posted 7 years ago
      • * saji says:

        Hi,

        I am trying to use journal codes in VB.Net. Could you please help in this regard. Please give step by step directions (if possible). Consider I am a new comer in this UG Customization.

        Regards,
        Sajith

        Posted 6 years, 12 months ago
      • * Szücs Ferenc says:

        Dear Grzegorz,

        I am trying to make an executable file in vb.NET acc.to it is written in NXOpen examplesREADME.txt, but I always get a BadImageFormatException when I execute it.

        Can you help me with this? I am using vs2010 and NX7.5.

        THX.

        Posted 3 years, 11 months ago
  3. * Grzegorz says:

    Sajith, sorry for a long respond time. Finally I managed it. I’ve uploaded the video to youtube where you can have a look what has been done. The link is: http://www.youtube.com/watch?v=GECtrIAR6dM . There is a windows form and you can load an xml based cubes to NX. You can also create/modify the xml file using grid information. At last you can upload the xml file as cubes to you NX environment. That’s quite nice in my opinion. There is also source code and executable to this video I’ve prepared: sourcecode: http://rapidshare.com/files/430552708/UGS-Cubes-src.zip, executable: http://rapidshare.com/files/430552996/UGS-Cubes-executable.zip . This example has been written in C# but VB.NET would be very very similar and you shouldn’t have any problems with code understanding. If you want to know how to do the tutorial from above in VB.net change your macrorecording language to VB.net/C#, record what you want and than open sourcecode to my NX cubes example and make your own customization on it. This should work. I hope I helped you.
    Regards,
    Grzegorz

    | Reply Posted 6 years, 11 months ago
  4. * Samuel says:

    Hi,
    so I have the C++ .cpp file now. What should be done to import it into NX 7.5?

    Thanks

    | Reply Posted 5 years, 10 months ago
    • * Grzegorz says:

      You should compile either dll or exe and than from main menu file->execute->nx open and choose your compiled file (dll or exe). It should work.

      | Reply Posted 5 years, 10 months ago
  5. * tay says:

    Thanks for that tutorial. I just started using NX6 and found the http://www.nxtutorials.com website to have many interesting articles, tutorials, and models. I hope you find it helpful also!

    | Reply Posted 5 years, 10 months ago
  6. * matt says:

    Do You have source files for your example from post? I’m trying to compile simple dll file but always I get errors. Have You managed to add NX Open Wizard to VS 2010? If not what settings (paths, directories etc) should be added to the project?

    Thanks in advance for Your reply.

    | Reply Posted 5 years, 9 months ago
    • * Grzegorz says:

      Hi Matt!
      I hope I still have sources and I will send you with my email. The problem is that I shared them on rapidshare and they are deleting files from accounts every x months. External dll and libs files are shown in the post. Please have a look on the picture: https://cadcammodelling.files.wordpress.com/2010/06/cylinder3.png . The directories and so on: On my computer the directories are following: C:\Program Files (x86)\UGS\NX6.0\UGOpen for pure C UGS programming and C:\Program Files (x86)\UGS\NX6.0\UGOpen\NXOpen for C++ programming with a different framework. I have somewhere described the differences between both of them. The runtime dll files are at my computer here: C:\Program Files (x86)\UGS\NX 6.0\UGII. If you cannot find the directories on your computer than you have to find where is NXOpen.UF.dll and than you will find the dll directories and uf.h and than you will find source dirs and header dirs.
      Best Regards,
      Grzegorz

      | Reply Posted 5 years, 9 months ago
  7. * Hank says:

    Hi Gregorz,

    can you send me more samples, too? I am also interested if you have any explanation/sample of how to make an executable NX Open file (exe).

    Thanks for all your work

    | Reply Posted 5 years, 8 months ago
    • * Grzegorz says:

      Hi Hank!
      I was on my winter vacation, so please accept my apologies for a delay in answer.
      Building exe file is not much more difficult than building a dll file. You simply have to build exe, the all linking libraries remains the same as with dll. Then you execute exe. The main difference between exe and dll is that dll is working always within running NX. Exe can run NX in the background, even without showing NX / starting NX. So exe can be stand-alone, can be runned within NX environment like dll. Dll can be only runned within working NX. You choose what you want. I had an app with C# NX Open exe file. I will send you the source code by email.
      Best Reagrds,
      Grzegorz

      | Reply Posted 5 years, 7 months ago
  8. been thinking of trying my hand at compiling a small app for nx, and I was wondering if Visual Studio is the only build environment you can use, or do you know if one of the open source IDEs can be used?

    | Reply Posted 5 years, 7 months ago
    • * Grzegorz says:

      No, you are not limited to Visual Studio. You can use Eclipse as well, G++/GCC with any open source environment. Eclipse is especially valuable tool for java developers (you can write java nx customization software). The only difficulty is to configure your environment (all the libs, compilation, linking etc). NX sets no limits to the programming environment. There are also no project templates that comes within NX by default. This is a drawback, because this work has to be done on your own. There is no priviliged environment and you choose the one suitable for you.
      Best Regards,
      Grzegorz

      | Reply Posted 5 years, 7 months ago
      • * Daniel Ullfig says:

        Thanks for your reply! I’ve been experimenting a bit using Dev-C++ (an open source IDE). The initial learning curve is quite steep. The program compiles fine, but the main problem is figuring out which libraries need to get linnked in with the output. I keep getting error messages telling me that it can’t find certain functions.

        Posted 5 years, 7 months ago
  9. * bubba kittee says:

    For you beginners …
    I’d suggest that using VB or C# would be a lot easier than C++.
    As an IDE, I’d recommend either a Vis Studio Express edition, or SharpDevelop. Both are free.

    The easiest programming tool to use with NX is SNAP (a new API in NX8). It’s based on VB. An easy-to-read “SNAP Getting Started Guide” is provided as part of the standard NX docs.

    | Reply Posted 5 years, 7 months ago
    • * Isha says:

      i am doing cad cam (modeling & mfg) since 6 yrs I am interested in cad cam customization but i am so confused from where should i start i should do some courses or if so which courses and from where
      Pls help me

      | Reply Posted 5 years, 2 months ago
  10. * Isha says:

    pls reply

    | Reply Posted 5 years, 2 months ago
    • * Grzegorz says:

      Well, there are unfortunately limited resources over the internet. Especially from programmatical customizations. What I suggest is to get some project to work on and read documentation from NX Automation that comes together with NX. And then start working. The very basics – how to start – you can find on my blog. It’s working. Try to reproduce my cylinder and then download the cubes. Cubes are also interesting because I am attaching .NET WinForms software to NX Session. There are source codes provided to every blog entry. If you find a broken link, let me know. Rapidshare where I have uploaded source codes deletes automatically files after half a year.
      This should give some spotlight onto programming in NX, providing you know how to use software documentation. Provided NX documentation is not a step by step tutorial. This documentation has been written like every documentation for software library. Any experience in open source software development greatly facilitate the job.

      Another possibility is to attend a course from Siemens. There are ones in Switzerland. They are also pricey , but if you are in a dire straight probably it is an option to attend a course and then sell your software at higher price.
      I am getting many questions regarding on how to start with NX Customization.

      What you can do is to download any NX model from Grabcad and then try to modify it using NX customization. This could be nice experience.
      Places where you can get some tutorials about NX, but without NX customizations tutorials:
      http://www.3dcadtips.com/

      http://ugs-tutor.blogspot.ch/

      There are lots of things still to publish on this blog. But the problem is that I have to complete my professional job at first and I am fully booked until September 2013. I am an independend software developer completing CAD/CAM projects. You are welcome to see my website: http://www.parametricgeometry.com . Somewhere in September 2013, or in Summer 2013 I will extend the NX programming section. I was also thinking about writing a book or smth, but this all is unclear for now as I do have little time. My intention of this blog was to publish knowledge in pure mathematical CAD/CAM development and in NX customization. I will stick to this vision and come back to further writing because the more knowledge source we (as civilization have), the better. If you have any ideas how could I easily help people (easily = working hours only on saturdays), you are welcome to write me a message and get in touch with me. We will discuss it.

      | Reply Posted 5 years, 2 months ago
      • * Isha says:

        Thanks Grzegorz
        thank you so much

        Posted 5 years, 2 months ago
  11. * mark says:

    Is it possible to relaod http://rapidshare.com/files/430552708/UGS-Cubes-src.zip ?

    | Reply Posted 5 years, 2 months ago
    • * Grzegorz says:

      Yes. I will do it tomorrow and send you an update. Please wait until tomorrow. I have found this sample code yesterday by coincidence.

      | Reply Posted 5 years ago
      • * mhk99 says:

        Is it possible to relaod UGS-Cubes-src.zip ?

        Posted 4 years, 5 months ago
  12. * ashish says:

    I have a good command in UGS NX 7.5 and the latest versions for the same, but do not have any knowledge about programing in such softwares,
    can you please help me in starting such programmy things in UGS

    regards,
    ashish k. s.

    | Reply Posted 4 years, 11 months ago
  13. * Ramu Kandregula says:

    Nice Explanation. Thank you very much

    | Reply Posted 4 years, 11 months ago
  14. * DavidQ says:

    Hello Grzegorz:
    I have a question regarding the source code for a UserForm build in VisualBasic 2010 ExpEd, how do you made the transfer/compile the form to sourcecode/journal form from VB? I know that I can write each object/propierties but this take too much time, so there is a faster way to do it?.
    I can modify some UserForm examples but some time the changes aren’t done correctly, like a botton location.

    BestRegards…Excellent article, very helpful!!!

    | Reply Posted 4 years, 9 months ago
  15. * Rathan says:

    Hello Mr.Grzegorz,
    The information regarding NX Customization that you are providing is very helpful for beginners like myself.
    Please provide email ID it will be very helpful to discuss any queries.

    Thanks in advance.

    | Reply Posted 3 years, 12 months ago
    • * Grzegorz says:

      You can always write me. Use following address: my surname: pytel and then @gmx.com

      | Reply Posted 3 years, 10 months ago


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s