Beginners Guide to Learn NXOpen Programming
Beginners Guide to Learn NXOpen Programming
Table of Contents
Purpose of this ebook
Table of Contents
Introduction to NX Open
Unigraphics
NX programming Tools
What is NX Open?
UFUNC or User Function
Programming Language for CAD Automation: C Sharp or Visual Basic .Net
#1
CREATE BLANK PART WITH DRAWING SHEET (C Sharp)
Source code:
Explanation:
#2
UPDATE ALL SOLID EDGE COMPONENTS (Visual Basic .Net)
Purpose:
How to use:
Source Code:
Explanation:
#3
REPORT TOP LEVEL PARTS IN SESSION (Visual Basic .Net)
Purpose:
How to use:
Source Code:
Explanation:
#4
REPORT ALL CUSTOM SYMBOLS (Visual Basic .Net)
Purpose:
How to use:
Source Code:
Explanation:
What is CAD API | Application Programming Interface | Computer Aided Design
#5
UPDATE ALL DRAFTING VIEWS (Visual Basic .Net)
Purpose:
How to use:
Source Code:
Explanation:
What is the Difference Between CAD Developer and Design Automation Engineer?
#6
EXPORT EXPRESSIONS FROM WORK PART (Visual Basic .Net)
Purpose:
How to use:
Source Code:
Explanation:
Stages of Learning CAD Automation / CAD Customization
#7
CLEAN UP WORK PART (Visual Basic .Net)
Purpose:
How to use:
Source code:
Explanation:
#8
SHOW ALL FEATURE DIMENSIONS (Visual Basic .Net)
Purpose:
How to use:
Source code:
Explanation:
CONCLUSION
Attend the NXOpen Programming Masterclass
Introduction to NX Open
Siemens NX does not need an introduction to any Mechanical Design Engineer. Most Design
engineers used it at some point of time.
Unigraphics
NX, formerly known as "Unigraphics", is an advanced high-end CAD/CAM/CAE software. For the
first time in the 1980s, it was considered the industry’s true interactive solid modeling software.
NX programming Tools
NX programming and customization software tool is the innovation that took shape in the early
1990s. NX programming and customization software tools help extend and tailor solution
capabilities to custom needs and also prevent product knowledge, all the way through analysis
and manufacturing processes.
Extras
What is NX Open?
Watch on Youtube - https://youtu.be/4cym4e9Tiqc
NXOpen is supporting applications developed in Java, Python, Visual Basic .NET, C# .Net, C/C++
Languages.
Extras
#1
CREATE BLANK PART WITH DRAWING SHEET (C
Sharp)
Source code:
using System;
using NXOpen;
FileNew fileNew1;
fileNew1 = theSession.Parts.FileNew();
fileNew1.TemplateFileName = "Blank";
fileNew1.Application = FileNewApplication.Gateway;
fileNew1.Units = NXOpen.Part.Units.Millimeters;
fileNew1.NewFileName = @"C:\temp\test.prt";
fileNew1.UseBlankTemplate = true;
fileNew1.MakeDisplayedPart = true;
NXObject nXObject1 = fileNew1.Commit();
fileNew1.Destroy();
Explanation:
using System;
using NXOpen;
The above piece of code represent s the import of System and NXOpen references. This is a
mandatory step to connect Journal with NX.
FileNew i s an object used for creating parts . There are several methods within this object such
as template, unit s , file name, etc. These methods are specifically assigned values as per need.
There are several object builders available to use. These builders are used to creating something
in NX. Here DrawingSheetBuilder i s creating new drawing sheets with given specifications .
Several methods within the builder are used to do so. Based on which builder i s being used,
there are certain methods declared specifically to make it run successfully.
1/8: Create Blank Part With Drawing Sheet
Watch on YouTube - 1/8: Create Blank Part With Drawing Sheet | NX Open Programming |…
#2
The workaround interactively is to edit the component properties and run "File -> Properties - >
Interoperability -> Update from file "
The Journal does the job, traverses all components, and counts the bodies.
Then it updates each component part and counts the bodies again.
In case the number of bodies changes, it will report that component name in the listing window.
How to use:
Copy the entire code from the below page and paste it into the Journal editor to run.
Module update_all_solid_edge_components
Sub Main()
lw.Open()
Dim root As Component = _
s.Parts.Display.ComponentAssembly.RootComponent
If Not root Is Nothing Then
lw.WriteLine(root.DisplayName())
reportChildrensBodyStatus(root, 1)
Else
lw.WriteLine("The displayed part is not an assembly.")
End If
End Sub
ufs.Modl.EditImportBodyFeature(cPart.Tag, _
UFModl.ImportBodyFeatureEditOption.ImportBodyFeatureUpdateLink, _
nothing)
num_bodies_before
End If
Catch ex As Exception
lw.WriteLine("Error: " & ex.Message)
End Try
reportChildrensBodyStatus(child, indent + 1)
Next
End Sub
End Module
Explanation:
Imports NXOpen.UF
Imports NXOpen.Assemblies
Along with the import of System and NXOpen references , UF and Assemblies references are
imported, because API interfaces within these namespaces are used in this code.
Sub Main()
lw.Open()
Dim root As Component = _
s.Parts.Display.ComponentAssembly.RootComponent
If Not root Is Nothing Then
lw.WriteLine(root.DisplayName())
reportChildrensBodyStatus(root, 1)
Else
lw.WriteLine("The displayed part is not an assembly.")
End If
End Sub
'For Each' loop t raver ses each child component from the assembly one by one, and counts the
bodies . Then it updates each component part using 'ufs.Modl.EditImpotBodyFeature' and
counts the bodies again. In case the number of bodies changes , it will report that component
name in the listing window.
2/8: Update All SolidEdge Components
Watch on YouTube - 2/8: Update All Solid Edge Components | NX Open Programming | P…
#3
Purpose:
From a given assembly, this Journal reports only top-level parts in the session.
How to use:
You must have an assembly active in the NX session to run this Journal.
Source Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Module report_top_level_parts_in_session
Sub Main()
Next
End Sub
End Module
Explanation:
For Each thisPart As Part In theSession.Parts.ToArray()
Dim occs() As Tag
ufs.Assem.AskOccsOfPart(NXOpen.Tag.Null, thisPart.Tag, occs)
If occs.GetUpperBound(0) < 0 Then
'this part has no parent, so announce it
Echo("TOP Part: " & thisPart.FullPath())
End If
Next
'For Each' loop traverses through all parts in an assembly. 'AskOccsOfPar t' Asks for tags of all
occurrences of "part" under the assembly of "parent_par t." If there i s no parent then that part is
displayed.
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
How to use:
Use a drawing with custom symbols present in it. Run Journal when the drawing is active in the
NX session.
Source Code:
Imports System
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.UF
Module report_all_custom_symbols
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow
Dim wp As Part = s.Parts.Work
Sub Main()
Dim symbol As NXOpen.Tag
lw.Open()
Dim custSymCol As CustomSymbolCollection =
s.Parts.Work.Annotations().CustomSymbols()
td.TextType))
End Sub
End Module
Explanation:
CustomSymbolCollection class represents a collection of custom symbol objects . Methods
within 'Cus toSymbol' returns information for e.g. symbol name. 'CustomSymbolData' class
contains methods for fetching details like scale, angle, position, etc. of the symbol.
'CustomSymbolTextData' represents custom symbol text data. There are 2 level loops used in
the above example. The f i r s t loop works on theCus tom symbol object, the second loop works
on the text data of the custom symbol object.
Extras
What is CAD API | Application Programming Interface |
Computer Aided Design
Watch on YouTube - https://youtu.be/BFpfOie8eoQ
#5
How to use:
Use a drawing with several views which need an update.
Source Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Drawings
Imports NXOpen.UI
Imports NXOpen.Utilities
Module update_all_out_of_date_drafting_views_explicitly
Sub Main()
For Each dwg As DrawingSheet In s.Parts.Display.DrawingSheets
If dwg.IsOutOfDate Then
For Each dv As DraftingView In dwg.GetDraftingViews()
If dv.IsOutOfDate Then
dv.Update()
End If
Next
End If
Next
End Sub
Return Session.LibraryUnloadOption.Immediately
End Function
End Module
Explanation:
Loop ensures that every drawing sheet in a drawing document i s covered. ' IsOutOfDate' is a
simple handy method to check the status whether this Drawings.DrawingSheet is out of date.
And i f found out of date, then another loop picks each drawing view and updates it.
#6
Source Code:
Option Strict Off
Imports NXOpen
Imports System
Imports System.IO
Imports System.Environment
Imports System.Windows.Forms
Module export_expressions_from_work_part
Sub Main()
workPart.Expressions.ExportToFile(ExpressionCollection.ExportMode.WorkPa
rt, _
filename, _
ExpressionCollection.SortType.AlphaNum)
End If
End Sub
result = SaveExpFileDlg.ShowDialog()
filename = SaveExpFileDlg.FileName
SaveExpFileDlg.Dispose()
Return result
End Function
End Module
Explanation:
'ExportToFile' method exports the expressions to a text file, or if 'file_name' is a null reference
(Nothing in Visual Basic), list the expressions in the listing window. save_file function checks if
an expression file with the same filename exists at a given location.
6/8: Export Expressions File
Watch on YouTube - 6/8: Export Expressions File | Parametric Krish | NX Open Programm…
Extras
#7
How to use:
Run this Journal on any given file.
Source code:
Option Strict Off
Imports System
Imports NXOpen
Module cleanup_work_part
Sub Main()
partCleanup1 = s.NewPartCleanup()
partCleanup1.TurnOffHighlighting = True
partCleanup1.DeleteUnusedObjects = True
partCleanup1.DeleteUnusedExpressions = True
partCleanup1.CleanupDraftingObjects = True
partCleanup1.CleanupFeatureData = True
partCleanup1.FixOffplaneSketchCurves = True
partCleanup1.CleanupMatingData = True
partCleanup1.DeleteUnusedFonts = True
partCleanup1.CleanupCAMObjects = True
partCleanup1.DoCleanup()
partCleanup1.Dispose()
Echo("Finished!")
End Sub
Return Session.LibraryUnloadOption.Immediately
End Function
End Module
Explanation:
'PartCleanup' class has members which can be used to perform clean up. Above code performs
clean up activities like;
#8
How to use:
Run this Journal on any given file.
Source code:
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.Features
Module show_all_feature_dimensions_of_selected_features
Sub Main()
selectedFeatures(ii).ShowDimensions()
Next
End Sub
theUI.SelectionManager.SelectFeatures(prompt,
Selection.SelectionFeatureType.Browsable, selectFeatures)
End Function
GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY
End Function
End Module
Explanation:
FeatureCollection Class represents a collection of features .
In order to retrieve selected objects from Journal code, the UI classes used. This call contains
various elements that represent the NX User Interface. SelectionManager property i s used for
the current session of NX.
CONCLUSION
NXOpen is vast in nature and a Programming and customization tool with powerful features. It
is deep indeed.
NXOpen can be learned or rather started without a guide but it has limitations. One cannot be
good at learning on their own.
NXOpen is not straightforward to learn because of its complex nature. Reason number one is
understanding of the NXOpen Object hierarchy. Reason number two is the use of namespaces
from UF, SNAP, etc. makes it more confusing for beginners.
One shall know interactive NX in detail to start programming it. If you don 't know the features
and functions of NX, how can you automate them?