342 lines
14 KiB
C#
342 lines
14 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using NXOpen;
|
|
using NXOpen.UF;
|
|
using OfficeOpenXml;
|
|
|
|
namespace NX_Blech
|
|
{
|
|
class EditExpressions
|
|
{
|
|
Session theSession = NXOpen.Session.GetSession();
|
|
|
|
public EditExpressions()
|
|
{
|
|
Session.UndoMarkId markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit_Expressions");
|
|
theSession.SetUndoMarkName(markId1, "Edit_Expression");
|
|
|
|
if (Edit())
|
|
{
|
|
theSession.DeleteUndoMark(markId1, "Edit_Expressions");
|
|
}
|
|
}
|
|
|
|
#region OwnMethodes
|
|
private bool Edit()
|
|
{
|
|
if (ThicknessValueExist())
|
|
{
|
|
//Aktualisieren
|
|
CorrectExpressions();
|
|
|
|
return false; //Kein Abbruch
|
|
}
|
|
else
|
|
{
|
|
//Wenn 'Sheet_Metal_Material_Thickness' nicht existiert, dann eine Meldung ausgeben, dass zuerst die Funktion
|
|
//'Material Blechbearbeitung' ausgeführt werden muss, damit die notwendigen Expression Werte angelegt werden
|
|
NoThicknessMessage();
|
|
|
|
return true; //Abbruch
|
|
}
|
|
}
|
|
|
|
private bool ThicknessValueExist()
|
|
{
|
|
bool thicknessValueExist = false;
|
|
|
|
foreach (Expression name in theSession.Parts.Work.Expressions)
|
|
{
|
|
if (name.Name == "Sheet_Metal_Material_Thickness")
|
|
{
|
|
thicknessValueExist = true;
|
|
}
|
|
}
|
|
|
|
return thicknessValueExist;
|
|
}
|
|
|
|
private void CorrectExpressions()
|
|
{
|
|
//Werte aus Excel tabelle holen
|
|
string excel = String.Concat(theSession.GetEnvironmentVariableValue("NX_SERVER_DIR"), "\\global\\06_ENGEL_NX_CUSTOMIZING\\Blech\\Blechstaerke_Relief_Eugen.xlsx");
|
|
string[] sheetMetalValues = new string[5];
|
|
sheetMetalValues = ReadRow(excel, ReadThickness());
|
|
string exprUnit = "MilliMeter";
|
|
|
|
//Korrektur wenn das Blech vor NX12 konstruiert wurde
|
|
//TODO: Abfrage ob notwendig
|
|
PreNX12Created(exprUnit, sheetMetalValues);
|
|
|
|
//Werte setzen/aktualisieren
|
|
UpdateExpressions(exprUnit, sheetMetalValues);
|
|
}
|
|
|
|
private void PreNX12Created(string exprUnit, string[] sheetMetalValues)
|
|
{
|
|
//Korrektur wenn das Blech vor NX12 konstruiert wurde
|
|
foreach (Expression name in theSession.Parts.Work.Expressions)
|
|
{
|
|
//Existiert der Wert 't', dann den Wert 'Sheet_Metal_Relief_Depth' auf '0' setzen und den Wert 't' löschen
|
|
if (name.Name == "t")
|
|
{
|
|
SetReliefDepth(exprUnit, sheetMetalValues[2]); //Den Wert Sheet_Metal_Relief_Depth auf 0 setzen
|
|
ChangeValue("t", "Sheet_Metal_Relief_Depth"); //Alle Expressions die den Wert 't' enthalten gegen den Wert 'Sheet_Metal_Relief_Depth' tauschen
|
|
DeleteExpression("t"); //Expression 't' löschen
|
|
}
|
|
|
|
//Existiert der Wert 'b', dann den Wert 'Sheet_Metal_Relief_Width' abhängig vom Wert 'Sheet_Metal_Material_Thickness' auf '1' oder '2' setzen und den Wert 'b' löschen
|
|
if (name.Name == "b")
|
|
{
|
|
SetReliefWidth(exprUnit, sheetMetalValues[1]); //Den Wert 'Sheet_Metal_Relief_Width' abhängig von der Materialstärke auf '1' oder '2' setzen
|
|
ChangeValue("b", "Sheet_Metal_Relief_Width"); //Alle Expressions die den Wert 'b' enthalten gegen den Wert 'Sheet_Metal_Relief_Width' tauschen
|
|
DeleteExpression("b"); //Expression 'b' löschen
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateExpressions(string exprUnit, string[] sheetMetalValues)
|
|
{
|
|
//Expression Werte setzen
|
|
SetExpressionValues(exprUnit, sheetMetalValues);
|
|
|
|
//Den Wert Sheet_Metal_Relief_Width setzen
|
|
SetReliefWidth(exprUnit, sheetMetalValues[1]);
|
|
|
|
//Den Wert Sheet_Metal_Relief_Depth setzen
|
|
SetReliefDepth(exprUnit, sheetMetalValues[2]);
|
|
|
|
//k-Faktor bei 10mm Blech von 0.28 auf 0.42 ändern
|
|
CorrectKFactor();
|
|
}
|
|
|
|
//private void ImportExpressionValues()
|
|
//{
|
|
// //Dateiname der Blech-Expression-Tabelle
|
|
// string sheetMetalExpressionValues = String.Concat(theSession.GetEnvironmentVariableValue("NX_SERVER_DIR"), "\\global\\06_ENGEL_NX_CUSTOMIZING\\Blech\\blech.exp");
|
|
|
|
// //Expression Werte importieren
|
|
// bool expModified;
|
|
// string[] errorMessages = new string[50];
|
|
// theSession.Parts.Work.Expressions.ImportFromFile(sheetMetalExpressionValues, ExpressionCollection.ImportMode.Replace, out expModified, out errorMessages);
|
|
//}
|
|
|
|
private void SetExpressionValues(string exprUnit, string[] sheetMetalValues)
|
|
{
|
|
//Wert 's' setzen
|
|
SetUnitlessExpressionValue("s", exprUnit, "format( \"%#2.1f\", Sheet_Metal_Material_Thickness, \"units\", \"unitless\" )");
|
|
|
|
//Wert 'db' setzen
|
|
SetExpressionValue("db", exprUnit, sheetMetalValues[3]);
|
|
|
|
//Wert 'o' setzen
|
|
SetExpressionValue("o", exprUnit, sheetMetalValues[4]);
|
|
}
|
|
|
|
private string[] ReadRow(string excel, string sheetMetalThickness)
|
|
{
|
|
string[] sheetMetalValues = new string[5];
|
|
|
|
try
|
|
{
|
|
var fi = new FileInfo(excel);
|
|
|
|
using (var p = new ExcelPackage(fi))
|
|
{
|
|
//Get the Worksheet created in the previous codesample.
|
|
var ws = p.Workbook.Worksheets["Werte"];
|
|
|
|
for (int i = 1; i <= ws.Dimension.End.Row; i++)
|
|
{
|
|
if (ws.Cells[i, 1].Value != null)
|
|
{
|
|
if (ws.Cells[i, 1].Value.ToString() == sheetMetalThickness)
|
|
{
|
|
sheetMetalValues[0] = ws.Cells[i, 1].Value.ToString();
|
|
sheetMetalValues[1] = ws.Cells[i, 2].Value.ToString();
|
|
sheetMetalValues[2] = ws.Cells[i, 3].Value.ToString();
|
|
sheetMetalValues[3] = ws.Cells[i, 4].Value.ToString();
|
|
sheetMetalValues[4] = ws.Cells[i, 5].Value.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show("Fehler");
|
|
}
|
|
return sheetMetalValues;
|
|
}
|
|
|
|
private string ReadThickness()
|
|
{
|
|
Expression thickness = theSession.Parts.Work.Expressions.FindObject("Sheet_Metal_Material_Thickness");
|
|
|
|
return thickness.Value.ToString(); //Gibt die Materialstärke zurück
|
|
}
|
|
|
|
private void SetUnitlessExpressionValue(string exprName, string exprUnit, string exprValue)
|
|
{
|
|
bool found = false;
|
|
|
|
foreach (Expression name in theSession.Parts.Work.Expressions)
|
|
{
|
|
if (name.Name == exprName)
|
|
{
|
|
//Einen Expression Wert setzen
|
|
Expression expression = theSession.Parts.Work.Expressions.FindObject(name.Name);
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject(exprUnit);
|
|
|
|
theSession.Parts.Work.Expressions.Edit(expression, exprValue);
|
|
|
|
found = true;
|
|
}
|
|
}
|
|
|
|
//Wenn die Expression noch nicht existiert, dann erzeugen
|
|
if (!found)
|
|
{
|
|
CreateUnitlessExpressionValue(exprName, exprUnit, exprValue);
|
|
}
|
|
}
|
|
|
|
private void SetExpressionValue(string exprName, string exprUnit, string exprValue)
|
|
{
|
|
bool found = false;
|
|
|
|
foreach (Expression name in theSession.Parts.Work.Expressions)
|
|
{
|
|
if (name.Name == exprName)
|
|
{
|
|
//Einen Expression Wert setzen
|
|
Expression expression = theSession.Parts.Work.Expressions.FindObject(name.Name);
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject(exprUnit);
|
|
Unit nullNXOpen_Unit = null;
|
|
theSession.Parts.Work.Expressions.EditWithUnits(expression, nullNXOpen_Unit, SetCommaToDot(exprValue));
|
|
|
|
found = true;
|
|
}
|
|
}
|
|
|
|
//Wenn die Expression noch nicht existiert, dann erzeugen
|
|
if (!found)
|
|
{
|
|
CreateExpressionValue(exprName, exprUnit, exprValue);
|
|
}
|
|
}
|
|
|
|
private void CreateUnitlessExpressionValue(string exprName, string exprUnit, string exprValue)
|
|
{
|
|
//Einen Expression Wert erzeugen
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject(exprUnit);
|
|
Expression expression;
|
|
string expr = String.Concat(exprName, "=", exprValue);
|
|
expression = theSession.Parts.Work.Expressions.CreateExpression("String", expr);
|
|
}
|
|
|
|
private void CreateExpressionValue(string exprName, string exprUnit, string exprValue)
|
|
{
|
|
//Einen Expression Wert erzeugen
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject(exprUnit);
|
|
Expression expression;
|
|
string expr = String.Concat(exprName, "=", SetCommaToDot(exprValue));
|
|
expression = theSession.Parts.Work.Expressions.CreateWithUnits(expr, unit);
|
|
}
|
|
|
|
private string SetCommaToDot(string exprValue)
|
|
{
|
|
//Sicherstellen, dass das Komma ein Punkt ist
|
|
try
|
|
{
|
|
double result = 0.0;
|
|
if (Double.TryParse(exprValue, out result))
|
|
{
|
|
exprValue = Convert.ToDouble(exprValue).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-us")); ;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
|
|
return exprValue;
|
|
}
|
|
|
|
private void SetReliefDepth(string exprUnit, string sheetMetalValues)
|
|
{
|
|
//Den Wert Sheet_Metal_Relief_Depth auf 0 setzen
|
|
Expression expression = theSession.Parts.Work.Expressions.FindObject("Sheet_Metal_Relief_Depth");
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject(exprUnit);
|
|
|
|
theSession.Parts.Work.Expressions.EditWithUnits(expression, unit, sheetMetalValues);
|
|
}
|
|
|
|
private void SetReliefWidth(string exprUnit, string sheetMetalValues)
|
|
{
|
|
//Den Wert Sheet_Metal_Relief_Width abhängig von der Materialstärke auf 1 oder 2 setzen
|
|
Expression expression = theSession.Parts.Work.Expressions.FindObject("Sheet_Metal_Relief_Width");
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject(exprUnit);
|
|
|
|
theSession.Parts.Work.Expressions.EditWithUnits(expression, unit, sheetMetalValues);
|
|
}
|
|
|
|
private void CorrectKFactor()
|
|
{
|
|
foreach (Expression name in theSession.Parts.Work.Expressions)
|
|
{
|
|
if (name.Name == "Sheet_Metal_Material_Thickness")
|
|
{
|
|
Expression thickness = theSession.Parts.Work.Expressions.FindObject("Sheet_Metal_Material_Thickness");
|
|
|
|
if (thickness.Value == 10)
|
|
{
|
|
//Wenn die Blechdicke 10mm ist, dann den Wert 'Sheet_Metal_Neutral_Factor' von '0.28' auf '0.42' setzen
|
|
Expression expression = theSession.Parts.Work.Expressions.FindObject("Sheet_Metal_Neutral_Factor");
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject("MilliMeter");
|
|
string kFactor = "0.42";
|
|
|
|
theSession.Parts.Work.Expressions.EditWithUnits(expression, unit, kFactor);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ChangeValue(string value, string newValue)
|
|
{
|
|
//Alle Expressions die den Wert 'value' enthalten gegen den Wert 'newValue' tauschen
|
|
foreach (Expression name in theSession.Parts.Work.Expressions)
|
|
{
|
|
if (name.RightHandSide == value)
|
|
{
|
|
Expression expression = theSession.Parts.Work.Expressions.FindObject(name.Name);
|
|
Unit unit = theSession.Parts.Work.UnitCollection.FindObject("MilliMeter");
|
|
theSession.Parts.Work.Expressions.EditWithUnits(expression, unit, newValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DeleteExpression(string value)
|
|
{
|
|
//Expression löschen
|
|
Expression expression = theSession.Parts.Work.Expressions.FindObject(value);
|
|
|
|
theSession.Parts.Work.Expressions.Delete(expression);
|
|
}
|
|
|
|
private void NoThicknessMessage()
|
|
{
|
|
if (theSession.GetEnvironmentVariableValue("UGII_LANG") == "german")
|
|
{
|
|
//deutsch
|
|
NXOpen.UI.GetUI().NXMessageBox.Show("Warnung!", NXMessageBox.DialogType.Warning, "Bitte zuerst die Funktion \"Material Blechbearbeitung\"\nund dann noch einmal \"ENGEL_Blechparameter\" ausführen!");
|
|
}
|
|
else
|
|
{
|
|
//englisch
|
|
NXOpen.UI.GetUI().NXMessageBox.Show("Warning!", NXMessageBox.DialogType.Warning, "Please execute the function \"Material Blechbearbeitung\"\nfirst and then \"ENGEL_Blechparameter\" again!");
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|