Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

journal runnig in assembly

Status
Not open for further replies.

moog2

Mechanical
Jan 16, 2007
441
0
0
AU
Could someone please help modify this journal to run in an assembly.
I've got hundreds of files to run it on, and it'd be much better if the journal could run from the top level assembly, looking for all files with this expression, and adding the linked attribute accordingly.

This is so I can export to a spreadsheet the assembly navigator info with the sheet metal thickness in there too.

Any help much appreciated......

' NX 8.0.3.4
' Journal created by paulhorton on Fri Jan 29 10:28:58 2016 E. Australia Standard Time
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: File->Properties
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim objects1(0) As NXObject
objects1(0) = workPart
Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
attributePropertiesBuilder1 = workPart.PropertiesManager.CreateAttributePropertiesBuilder(objects1)

attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Object

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String

attributePropertiesBuilder1.Units = "MilliMeter"

Dim objects2(0) As NXObject
objects2(0) = workPart
Dim massPropertiesBuilder1 As MassPropertiesBuilder
massPropertiesBuilder1 = workPart.PropertiesManager.CreateMassPropertiesBuilder(objects2)

Dim selectNXObjectList1 As SelectNXObjectList
selectNXObjectList1 = massPropertiesBuilder1.SelectedObjects

Dim objects3() As NXObject
objects3 = selectNXObjectList1.GetArray()

massPropertiesBuilder1.UpdateOnSave = MassPropertiesBuilder.UpdateOptions.Yes

massPropertiesBuilder1.LoadPartialComponents = True

massPropertiesBuilder1.Accuracy = 0.99

Dim objects4(0) As NXObject
objects4(0) = workPart
Dim previewPropertiesBuilder1 As PreviewPropertiesBuilder
previewPropertiesBuilder1 = workPart.PropertiesManager.CreatePreviewPropertiesBuilder(objects4)

theSession.SetUndoMarkName(markId1, "Displayed Part Properties Dialog")

attributePropertiesBuilder1.DateValue.DateItem.Day = DateItemBuilder.DayOfMonth.Day29

attributePropertiesBuilder1.DateValue.DateItem.Month = DateItemBuilder.MonthOfYear.Jan

attributePropertiesBuilder1.DateValue.DateItem.Year = "2016"

attributePropertiesBuilder1.DateValue.DateItem.Time = "00:00:00"

attributePropertiesBuilder1.Units = "MilliMeter"

previewPropertiesBuilder1.StorePartPreview = True

previewPropertiesBuilder1.StoreModelViewPreview = True

previewPropertiesBuilder1.ModelViewCreation = PreviewPropertiesBuilder.ModelViewCreationOptions.OnViewSave

attributePropertiesBuilder1.Category = "Materials"

attributePropertiesBuilder1.Title = "thickness"

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number

attributePropertiesBuilder1.Units = "MilliMeter"

' ----------------------------------------------
' Dialog Begin Expressions
' ----------------------------------------------
attributePropertiesBuilder1.Units = "MilliMeter"

attributePropertiesBuilder1.Units = "MilliMeter"

attributePropertiesBuilder1.NumberValue = 4.0

attributePropertiesBuilder1.IsReferenceType = False

Dim expression1 As Expression = CType(workPart.Expressions.FindObject("Sheet_Metal_Material_Thickness"), Expression)

attributePropertiesBuilder1.Expression = expression1

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Displayed Part Properties")

Dim nXObject1 As NXObject
nXObject1 = attributePropertiesBuilder1.Commit()

Dim updateoption1 As MassPropertiesBuilder.UpdateOptions
updateoption1 = massPropertiesBuilder1.UpdateOnSave

Dim nXObject2 As NXObject
nXObject2 = massPropertiesBuilder1.Commit()

workPart.PartPreviewMode = BasePart.PartPreview.OnSave

Dim nXObject3 As NXObject
nXObject3 = previewPropertiesBuilder1.Commit()

theSession.DeleteUndoMark(markId2, Nothing)

theSession.SetUndoMarkName(markId1, "Displayed Part Properties")

attributePropertiesBuilder1.Destroy()

massPropertiesBuilder1.Destroy()

previewPropertiesBuilder1.Destroy()

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module
 
Replies continue below

Recommended for you

Have a look at the excellent NX Journaling site.

There is this tutorial on the very topic: Creating a Subroutine to Process all Components in an Assembly
Link

Paul

Paul Turner
CAD & Process Engineer
Mastip Technology
 
thanks, I had a look at that one but it adds the attribute to all files in the assembly, this will need to check the files for the expression first, else it'll error.
 
Try this.
Code is created in C#.

Open the entire assembly and run it.

// --> Copy this code into the .cs file

using System;
using NXOpen;
using NXOpen.Assemblies;

public class Program
{
// class members
private static Session theSession;
public static Program theProgram;
public static bool isDisposeCalled;

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
public Program()
{
try
{
theSession = Session.GetSession();
isDisposeCalled = false;
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
}
}

//------------------------------------------------------------------------------
// Explicit Activation
// This entry point is used to activate the application explicitly
//------------------------------------------------------------------------------
public static int Main(string[] args)
{
int retValue = 0;
try
{
theProgram = new Program();

const string expressionName = "Sheet_Metal_Material_Thickness";
const string attributeTitle = "thickness";
const string attributeCategory = "Materials";

Part[] parts = theSession.Parts.ToArray();

Part workPart = theSession.Parts.Work;

PartLoadStatus partLoadStatus1;
foreach (var item in parts)
{
theSession.Parts.SetDisplay(item, true, true, out partLoadStatus1);
CreateLinkedAttributeInWorkPart(expressionName, attributeTitle, attributeCategory);
}

theSession.Parts.SetWork(workPart);

theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
PrintInInfoWindow("ERROR :" + ex.ToString());
}
return retValue;
}

//------------------------------------------------------------------------------
// Following method disposes all the class members
//------------------------------------------------------------------------------
public void Dispose()
{
try
{
if (isDisposeCalled == false)
{
//TODO: Add your application code here
}
isDisposeCalled = true;
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----

}
}

public static int GetUnloadOption(string arg)
{
//Unloads the image explicitly, via an unload dialog
//return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);

//Unloads the image immediately after execution within NX
return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);

//Unloads the image when the NX session terminates
// return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
}

/// <summary>
/// Creates the linked attribute in work part
/// </summary>
/// <param name="expName">Expression name</param>
/// <param name="attributeTitle">Title of the attribute</param>
/// <param name="attributeCategory">attribute category. pass empty string if no category</param>
private static void CreateLinkedAttributeInWorkPart(string expName, string attributeTitle, string attributeCategory)
{
string partName = System.IO.Path.GetFileNameWithoutExtension(theSession.Parts.Work.FullPath);

Expression exp1 = GetExpressionByName(expName);

if (exp1 != null)
{
try
{
CreateExpressionLinkedAttribute(exp1, attributeTitle, attributeCategory);
}
catch
{
PrintInInfoWindow(string.Format("{0} linked attribute failed to create in {1} part", attributeTitle, partName));
}
}
else
{
PrintInInfoWindow(string.Format("{0} expression is not found in {1} part", expName, partName));
}
}

/// <summary>
/// Print the message in listing window
/// </summary>
/// <param name="message"></param>
private static void PrintInInfoWindow(string message)
{
if (!theSession.ListingWindow.IsOpen)
{
theSession.ListingWindow.Open();
}

theSession.ListingWindow.WriteLine(message);
}

/// <summary>
/// Gets a Expression by expression name.
/// </summary>
/// <param name="expressionName">Name of the expression</param>
/// <returns>Null value if no expression found with specified name</returns>
private static Expression GetExpressionByName(string expressionName)
{
Expression exp1 = null;

Part workPart = theSession.Parts.Work;

foreach (Expression item in workPart.Expressions)
{
if (item.Name.ToUpperInvariant() == expressionName.Trim().ToUpperInvariant())
{
exp1 = item;
}
}

return exp1;
}

/// <summary>
/// Creates a Expression linked Attribute
/// </summary>
/// <param name="exp1">Expression object</param>
/// <param name="attributeTitle">Title of the attribute</param>
/// <param name="attributeCategory">Category of the attribute. pass empty string if no category</param>
private static void CreateExpressionLinkedAttribute(Expression exp1, string attributeTitle, string attributeCategory)
{
Session theSession = Session.GetSession();
Part workPart = theSession.Parts.Work;

NXOpen.Session.UndoMarkId id1;
id1 = theSession.GetNewestUndoMark(NXOpen.Session.MarkVisibility.Visible);

NXObject[] objects1 = new NXObject[1];
objects1[0] = workPart;
AttributePropertiesBuilder attributePropertiesBuilder1;
attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.None);

attributePropertiesBuilder1.IsArray = false;

NXObject[] objects5 = new NXObject[1];
objects5[0] = workPart;
attributePropertiesBuilder1.SetAttributeObjects(objects5);

attributePropertiesBuilder1.DataType = NXOpen.AttributePropertiesBaseBuilder.DataTypeOptions.Number;

attributePropertiesBuilder1.Units = exp1.Units.JournalIdentifier;

attributePropertiesBuilder1.IsReferenceType = false;

attributePropertiesBuilder1.Expression = exp1;

attributePropertiesBuilder1.Title = attributeTitle;
attributePropertiesBuilder1.Category = attributeCategory;
attributePropertiesBuilder1.Commit();

theSession.UpdateManager.DoUpdate(id1);

attributePropertiesBuilder1.Destroy();
}
}

 
Thanks so much guys for helping me out....

Cowski, the journal works, but is there a way to get it to dig down into the sub assemblies?
it only works on the workpart assembly, some components I have are mirror wave links (with the parent assembled in the part), I have to be workpart on those as well to get the attribute in them.

SelvarajC,
I tried to run this but got this error.. something about attributes builder 1
I tried removing the "1" from those lines but still it gave an error...

attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.None);

attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.None);

attributePropertiesBuilder1.SetAttributeObjects(objects5);

hope you can find time to tweek the code, Thanks..
 
Try this..
If you still get the error. please attach the snapshot of the error.

using System;
using NXOpen;
using NXOpen.Assemblies;

public class Program
{
// class members
private static Session theSession;
public static Program theProgram;
public static bool isDisposeCalled;

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
public Program()
{
try
{
theSession = Session.GetSession();
isDisposeCalled = false;
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
}
}

//------------------------------------------------------------------------------
// Explicit Activation
// This entry point is used to activate the application explicitly
//------------------------------------------------------------------------------
public static int Main(string[] args)
{
int retValue = 0;
try
{
theProgram = new Program();

const string expressionName = "Sheet_Metal_Material_Thickness";
const string attributeTitle = "thickness";
const string attributeCategory = "Materials";

Part[] parts = theSession.Parts.ToArray();

Part workPart = theSession.Parts.Work;

PartLoadStatus partLoadStatus1;
foreach (var item in parts)
{
theSession.Parts.SetDisplay(item, true, true, out partLoadStatus1);
CreateLinkedAttributeInWorkPart(expressionName, attributeTitle, attributeCategory);
}

theSession.Parts.SetWork(workPart);

theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
PrintInInfoWindow("ERROR :" + ex.ToString());
}
return retValue;
}

//------------------------------------------------------------------------------
// Following method disposes all the class members
//------------------------------------------------------------------------------
public void Dispose()
{
try
{
if (isDisposeCalled == false)
{
//TODO: Add your application code here
}
isDisposeCalled = true;
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----

}
}

public static int GetUnloadOption(string arg)
{
//Unloads the image explicitly, via an unload dialog
//return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);

//Unloads the image immediately after execution within NX
return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);

//Unloads the image when the NX session terminates
// return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
}

/// <summary>
/// Creates the linked attribute in work part
/// </summary>
/// <param name="expName">Expression name</param>
/// <param name="attributeTitle">Title of the attribute</param>
/// <param name="attributeCategory">attribute category. pass empty string if no category</param>
private static void CreateLinkedAttributeInWorkPart(string expName, string attributeTitle, string attributeCategory)
{
string partName = System.IO.Path.GetFileNameWithoutExtension(theSession.Parts.Work.FullPath);

Expression exp1 = GetExpressionByName(expName);

if (exp1 != null)
{
try
{
CreateExpressionLinkedAttribute(exp1, attributeTitle, attributeCategory);
}
catch
{
PrintInInfoWindow(string.Format("{0} linked attribute failed to create in {1} part", attributeTitle, partName));
}
}
else
{
PrintInInfoWindow(string.Format("{0} expression is not found in {1} part", expName, partName));
}
}

/// <summary>
/// Print the message in listing window
/// </summary>
/// <param name="message"></param>
private static void PrintInInfoWindow(string message)
{
if (!theSession.ListingWindow.IsOpen)
{
theSession.ListingWindow.Open();
}

theSession.ListingWindow.WriteLine(message);
}

/// <summary>
/// Gets a Expression by expression name.
/// </summary>
/// <param name="expressionName">Name of the expression</param>
/// <returns>Null value if no expression found with specified name</returns>
private static Expression GetExpressionByName(string expressionName)
{
Expression exp1 = null;

Part workPart = theSession.Parts.Work;

foreach (Expression item in workPart.Expressions)
{
if (item.Name.ToUpperInvariant() == expressionName.Trim().ToUpperInvariant())
{
exp1 = item;
}
}

return exp1;
}

/// <summary>
/// Creates a Expression linked Attribute
/// </summary>
/// <param name="exp1">Expression object</param>
/// <param name="attributeTitle">Title of the attribute</param>
/// <param name="attributeCategory">Category of the attribute. pass empty string if no category</param>
private static void CreateExpressionLinkedAttribute(Expression exp1, string attributeTitle, string attributeCategory)
{
Session theSession = Session.GetSession();
Part workPart = theSession.Parts.Work;

NXOpen.Session.UndoMarkId id1;
id1 = theSession.GetNewestUndoMark(NXOpen.Session.MarkVisibility.Visible);

NXObject[] objects1 = new NXObject[1];
objects1[0] = workPart;
AttributePropertiesBuilder attributePropertiesBuilder1;
attributePropertiesBuilder1 = workPart.PropertiesManager.CreateAttributePropertiesBuilder(objects1);// theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.None);

attributePropertiesBuilder1.IsArray = false;

NXObject[] objects5 = new NXObject[1];
objects5[0] = workPart;
attributePropertiesBuilder1.SetAttributeObjects(objects5);

attributePropertiesBuilder1.DataType = NXOpen.AttributePropertiesBaseBuilder.DataTypeOptions.Number;

attributePropertiesBuilder1.Units = exp1.Units.JournalIdentifier;

attributePropertiesBuilder1.IsReferenceType = false;

attributePropertiesBuilder1.Expression = exp1;

attributePropertiesBuilder1.Title = attributeTitle;
attributePropertiesBuilder1.Category = attributeCategory;
attributePropertiesBuilder1.Commit();

theSession.UpdateManager.DoUpdate(id1);

attributePropertiesBuilder1.Destroy();
}
}
 
Two things of note about my code:
[ul]
[li]If the expression doesn't exist in the part, the attribute won't be created/updated.[/li]
[li]It will attempt to fully load your components. If the component could not be fully loaded, the attribute change may not "stick".[/li]
[/ul]

If neither of the above applies to your part(s), please post back with more details so I can track down what is happening.

www.nxjournaling.com
 
Cowski,
that's fine if the expression doesn't exist.
When I create a mirrored wave link part, I assemble the parent into it, do the link, then remove it from the reference set, I guess this could be the problem.
I only did a quick test last night, now I've noticed it only catches some of the parts it should do, in the assembly. Most missed parts have the expression in them, and running the original journal works on them.
I'm pretty sure the journal stops running when it finds a part without the expression.
Hope this helps..
 
Hi moog2,

Code is generated in NX 9. I thought it'll work for NX 8. but some methods have been changed in NX 9. So it is not allowing to compile in NX 8.

Remove the line no 186 and use the below line.
// attributePropertiesBuilder1.SetAttributeObjects(objects5); // To be removed.
attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Object;
 
Cowski,
Thanks very much that worked floorlessly, much appreciated.
That will be very useful,
I edited the code to add other attributes for me and it worked well.
I tried to put more than one in the same Journal, but couldn't get it to work, so did them separately instead.

Also thanks to SelvarajC for your time & help...
 
Status
Not open for further replies.
Back
Top