CADlalala
Aerospace
- Apr 3, 2014
- 43
Dear all,
I am trying to write a program in C# in order to automatically extract the faces of a given solid body and revolve all the extracted faces around the Z axis. My final aim is to get the 2D axisymmetric equivalent of any 3D solid body by cutting a section of the resulting set of revolved bodies. The problem I am facing is that some of the faces cannot be revolved (see presentation attached) and if I try to revolve edges instead of faces I get undesirable sliver surfaces.
Have any of you tried to do this before? Does anybody has a bit of code or an idea about how to solve this problem or work around it?
Attached there is a presentation with the (prt)and the code I have written so far is below and attached:
Many thanks,
J
using System;
using NXOpen;
using NXOpen.UF;
public class Program
{
// class members
private static Session theSession;
private static UI theUI;
private static UFSession theUfSession;
public static Program theProgram;
public static bool isDisposeCalled;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
public Program()
{
try
{
theSession = Session.GetSession();
theUI = UI.GetUI();
theUfSession = UFSession.GetUFSession();
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();
//TODO: Add your application code here
Session theSession = Session.GetSession();
Part workPart = theSession.Parts.Work;
Part displayPart = theSession.Parts.Display;
foreach (NXOpen.Body body in workPart.Bodies)
{
//Extract Faces
Face[] faces = body.GetFaces();
NXOpen.Features.Feature[] nullFeatures_FeatureArray = new NXOpen.Features.Feature[faces.Length];
NXOpen.Features.ExtractFaceBuilder[] extractFaceBuilderArray = new NXOpen.Features.ExtractFaceBuilder[faces.Length];
extractFaceBuilderArray[0] = workPart.Features.CreateExtractFaceBuilder(nullFeatures_FeatureArray[0]);
extractFaceBuilderArray[0].Type = NXOpen.Features.ExtractFaceBuilder.ExtractType.Face;
bool[] added = new bool[faces.Length];
added[0] = extractFaceBuilderArray[0].ObjectToExtract.Add(faces[0]);
NXObject[] nXobjects = new NXObject[faces.Length];
nXobjects[0] = extractFaceBuilderArray[0].Commit();
for (int i = 0; i < faces.Length + 1; i++)
{
extractFaceBuilderArray = workPart.Features.CreateExtractFaceBuilder(nullFeatures_FeatureArray);
extractFaceBuilderArray.ParentPart = NXOpen.Features.ExtractFaceBuilder.ParentPartType.WorkPart;
extractFaceBuilderArray.Type = NXOpen.Features.ExtractFaceBuilder.ExtractType.Face;
added = extractFaceBuilderArray.ObjectToExtract.Add(faces);
nXobjects = extractFaceBuilderArray.Commit();
NXOpen.Features.Feature nullFeatures_FeatureR = null;
Face[] faces2 = body.GetFaces();
Body[] targetBodies1 = new Body[1];
Body nullBody = null;
targetBodies1[0] = nullBody;
//Section
Section section1;
section1 = workPart.Sections.CreateSection(0.02413, 0.0254, 0.5);
//Origin, vector, direction and axis
Point3d origin1 = new Point3d(0.0, 0.0, 0.0);
Vector3d vector1 = new Vector3d(0.0, 0.0, 1.0);
Direction direction1;
direction1 = workPart.Directions.CreateDirection(origin1, vector1, NXOpen.SmartObject.UpdateOption.WithinModeling);
Point nullPoint = null;
Axis axis1;
axis1 = workPart.Axes.CreateAxis(nullPoint, direction1, NXOpen.SmartObject.UpdateOption.WithinModeling);
//Revolve Builder
NXOpen.Features.RevolveBuilder revolveBuilder1;
revolveBuilder1 = workPart.Features.CreateRevolveBuilder(nullFeatures_FeatureR);
revolveBuilder1.Limits.StartExtend.Value.RightHandSide = "0";
revolveBuilder1.Limits.EndExtend.Value.RightHandSide = "360";
revolveBuilder1.Limits.StartExtend.Value.RightHandSide = "0";
revolveBuilder1.Limits.EndExtend.Value.RightHandSide = "360";
revolveBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;
revolveBuilder1.BooleanOperation.SetTargetBodies(targetBodies1);
revolveBuilder1.Offset.StartOffset.RightHandSide = "0";
revolveBuilder1.Offset.EndOffset.RightHandSide = "5";
revolveBuilder1.Tolerance = 0.0254;
revolveBuilder1.Section = section1;
revolveBuilder1.Axis = axis1;
section1.DistanceTolerance = 0.0254;
section1.ChainingTolerance = 0.02413;
section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves);
//Faces
Face[] facesOfFeatures1 = new Face[1];
//NXOpen.Features.Brep brep1 = (NXOpen.Features.Brep)workPart.Features.FindObject("UNPARAMETERIZED_FEATURE(1)");
//Face face1 = (Face)brep1.FindObject("FACE 5 {(1789.8054883419686,-471.394872773486,-541.7998006456055) UNPARAMETERIZED_FEATURE(1)}");
facesOfFeatures1[0] = faces;
EdgeBoundaryRule edgeBoundaryRule1;
edgeBoundaryRule1 = workPart.ScRuleFactory.CreateRuleEdgeBoundary(facesOfFeatures1);
//Self-intersection
section1.AllowSelfIntersection(false);
//Necesary code to get the revolve
SelectionIntentRule[] rules1 = new SelectionIntentRule[1];
rules1[0] = edgeBoundaryRule1;
NXObject nullNXObject = null;
Point3d helpPoint1 = new Point3d(792.492921337962, -84.5426421780617, -623.454293084316);
//Revolve the section around the selected axis
section1.AddToSection(rules1, faces2[0], nullNXObject, nullNXObject, helpPoint1, NXOpen.Section.Mode.Create, false);
revolveBuilder1.Section = section1;
revolveBuilder1.Axis = axis1;
revolveBuilder1.ParentFeatureInternal = false;
NXOpen.Features.Feature feature1;
feature1 = revolveBuilder1.CommitFeature();
}
}
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
}
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);
}
}
I am trying to write a program in C# in order to automatically extract the faces of a given solid body and revolve all the extracted faces around the Z axis. My final aim is to get the 2D axisymmetric equivalent of any 3D solid body by cutting a section of the resulting set of revolved bodies. The problem I am facing is that some of the faces cannot be revolved (see presentation attached) and if I try to revolve edges instead of faces I get undesirable sliver surfaces.
Have any of you tried to do this before? Does anybody has a bit of code or an idea about how to solve this problem or work around it?
Attached there is a presentation with the (prt)and the code I have written so far is below and attached:
Many thanks,
J
using System;
using NXOpen;
using NXOpen.UF;
public class Program
{
// class members
private static Session theSession;
private static UI theUI;
private static UFSession theUfSession;
public static Program theProgram;
public static bool isDisposeCalled;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
public Program()
{
try
{
theSession = Session.GetSession();
theUI = UI.GetUI();
theUfSession = UFSession.GetUFSession();
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();
//TODO: Add your application code here
Session theSession = Session.GetSession();
Part workPart = theSession.Parts.Work;
Part displayPart = theSession.Parts.Display;
foreach (NXOpen.Body body in workPart.Bodies)
{
//Extract Faces
Face[] faces = body.GetFaces();
NXOpen.Features.Feature[] nullFeatures_FeatureArray = new NXOpen.Features.Feature[faces.Length];
NXOpen.Features.ExtractFaceBuilder[] extractFaceBuilderArray = new NXOpen.Features.ExtractFaceBuilder[faces.Length];
extractFaceBuilderArray[0] = workPart.Features.CreateExtractFaceBuilder(nullFeatures_FeatureArray[0]);
extractFaceBuilderArray[0].Type = NXOpen.Features.ExtractFaceBuilder.ExtractType.Face;
bool[] added = new bool[faces.Length];
added[0] = extractFaceBuilderArray[0].ObjectToExtract.Add(faces[0]);
NXObject[] nXobjects = new NXObject[faces.Length];
nXobjects[0] = extractFaceBuilderArray[0].Commit();
for (int i = 0; i < faces.Length + 1; i++)
{
extractFaceBuilderArray = workPart.Features.CreateExtractFaceBuilder(nullFeatures_FeatureArray);
extractFaceBuilderArray.ParentPart = NXOpen.Features.ExtractFaceBuilder.ParentPartType.WorkPart;
extractFaceBuilderArray.Type = NXOpen.Features.ExtractFaceBuilder.ExtractType.Face;
added = extractFaceBuilderArray.ObjectToExtract.Add(faces);
nXobjects = extractFaceBuilderArray.Commit();
NXOpen.Features.Feature nullFeatures_FeatureR = null;
Face[] faces2 = body.GetFaces();
Body[] targetBodies1 = new Body[1];
Body nullBody = null;
targetBodies1[0] = nullBody;
//Section
Section section1;
section1 = workPart.Sections.CreateSection(0.02413, 0.0254, 0.5);
//Origin, vector, direction and axis
Point3d origin1 = new Point3d(0.0, 0.0, 0.0);
Vector3d vector1 = new Vector3d(0.0, 0.0, 1.0);
Direction direction1;
direction1 = workPart.Directions.CreateDirection(origin1, vector1, NXOpen.SmartObject.UpdateOption.WithinModeling);
Point nullPoint = null;
Axis axis1;
axis1 = workPart.Axes.CreateAxis(nullPoint, direction1, NXOpen.SmartObject.UpdateOption.WithinModeling);
//Revolve Builder
NXOpen.Features.RevolveBuilder revolveBuilder1;
revolveBuilder1 = workPart.Features.CreateRevolveBuilder(nullFeatures_FeatureR);
revolveBuilder1.Limits.StartExtend.Value.RightHandSide = "0";
revolveBuilder1.Limits.EndExtend.Value.RightHandSide = "360";
revolveBuilder1.Limits.StartExtend.Value.RightHandSide = "0";
revolveBuilder1.Limits.EndExtend.Value.RightHandSide = "360";
revolveBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;
revolveBuilder1.BooleanOperation.SetTargetBodies(targetBodies1);
revolveBuilder1.Offset.StartOffset.RightHandSide = "0";
revolveBuilder1.Offset.EndOffset.RightHandSide = "5";
revolveBuilder1.Tolerance = 0.0254;
revolveBuilder1.Section = section1;
revolveBuilder1.Axis = axis1;
section1.DistanceTolerance = 0.0254;
section1.ChainingTolerance = 0.02413;
section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves);
//Faces
Face[] facesOfFeatures1 = new Face[1];
//NXOpen.Features.Brep brep1 = (NXOpen.Features.Brep)workPart.Features.FindObject("UNPARAMETERIZED_FEATURE(1)");
//Face face1 = (Face)brep1.FindObject("FACE 5 {(1789.8054883419686,-471.394872773486,-541.7998006456055) UNPARAMETERIZED_FEATURE(1)}");
facesOfFeatures1[0] = faces;
EdgeBoundaryRule edgeBoundaryRule1;
edgeBoundaryRule1 = workPart.ScRuleFactory.CreateRuleEdgeBoundary(facesOfFeatures1);
//Self-intersection
section1.AllowSelfIntersection(false);
//Necesary code to get the revolve
SelectionIntentRule[] rules1 = new SelectionIntentRule[1];
rules1[0] = edgeBoundaryRule1;
NXObject nullNXObject = null;
Point3d helpPoint1 = new Point3d(792.492921337962, -84.5426421780617, -623.454293084316);
//Revolve the section around the selected axis
section1.AddToSection(rules1, faces2[0], nullNXObject, nullNXObject, helpPoint1, NXOpen.Section.Mode.Create, false);
revolveBuilder1.Section = section1;
revolveBuilder1.Axis = axis1;
revolveBuilder1.ParentFeatureInternal = false;
NXOpen.Features.Feature feature1;
feature1 = revolveBuilder1.CommitFeature();
}
}
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
}
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);
}
}