NX二次開發-建模-獲取面的相切曲面

NX二次開發專注者發表於2020-11-11

在UG NX中,選擇面時,有相切曲面、體的面等等。

 

在二次開發過程中,為了獲得面的相切面,採用錄製功能,獲取獲取相切面的程式碼(以偏置曲面為例),獲得如下程式碼。

// NX 1934
// Journal created by Administrator on Wed Nov 11 23:48:21 2020 中國標準時間

//
using System;
using NXOpen;

public class NXJournal
{
  public static void Main(string[] args)
  {
    NXOpen.Session theSession = NXOpen.Session.GetSession();
    NXOpen.Part workPart = theSession.Parts.Work;
    NXOpen.Part displayPart = theSession.Parts.Display;
    // ----------------------------------------------
    //   選單:插入(S)->偏置/縮放(O)->偏置曲面(O)...
    // ----------------------------------------------
    NXOpen.Session.UndoMarkId markId1;
    markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "起點");
    
    NXOpen.Features.Feature nullNXOpen_Features_Feature = null;
    NXOpen.Features.OffsetSurfaceBuilder offsetSurfaceBuilder1;
    offsetSurfaceBuilder1 = workPart.Features.CreateOffsetSurfaceBuilder(nullNXOpen_Features_Feature);
    
    NXOpen.Unit unit1;
    unit1 = offsetSurfaceBuilder1.Radius.Units;
    
    NXOpen.Expression expression1;
    expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("5", unit1);
    
    offsetSurfaceBuilder1.OutputOption = NXOpen.Features.OffsetSurfaceBuilder.OutputOptionType.OneFeatureForAllFaces;
    
    offsetSurfaceBuilder1.Tolerance = 0.001;
    
    offsetSurfaceBuilder1.Radius.SetFormula("5");
    
    offsetSurfaceBuilder1.SetOrientationMethod(NXOpen.Features.OffsetSurfaceBuilder.OrientationMethodType.UseExistingNormals);
    
    offsetSurfaceBuilder1.ApproxOption = true;
    
    offsetSurfaceBuilder1.PartialOption = true;
    
    offsetSurfaceBuilder1.MaximumExcludedObjects = 10;
    
    offsetSurfaceBuilder1.RemoveProblemVerticesOption = true;
    
    theSession.SetUndoMarkName(markId1, "偏置曲面 對話方塊");
    
    NXOpen.ScCollector nullNXOpen_ScCollector = null;
    NXOpen.GeometricUtilities.FaceSetOffset faceSetOffset1;
    faceSetOffset1 = workPart.FaceSetOffsets.CreateFaceSet("5", nullNXOpen_ScCollector, false, 0);
    
    offsetSurfaceBuilder1.FaceSets.Append(faceSetOffset1);
    
    NXOpen.ScCollector scCollector1;
    scCollector1 = workPart.ScCollectors.CreateCollector();
    
    NXOpen.Features.SweepAlongGuide sweepAlongGuide1 = ((NXOpen.Features.SweepAlongGuide)workPart.Features.FindObject("SWEEP(2)"));
    NXOpen.Face face1 = ((NXOpen.Face)sweepAlongGuide1.FindObject("FACE 563448 {(-8.5786437626905,0,191.4213562373095) SWEEP(2)}"));
    NXOpen.Face[] boundaryFaces1 = new NXOpen.Face[0];
    NXOpen.FaceTangentRule faceTangentRule1;
    faceTangentRule1 = workPart.ScRuleFactory.CreateRuleFaceTangent(face1, boundaryFaces1, 0.050000000000000003);
    
    NXOpen.SelectionIntentRule[] rules1 = new NXOpen.SelectionIntentRule[1];
    rules1[0] = faceTangentRule1;
    scCollector1.ReplaceRules(rules1, false);
    
    faceSetOffset1.FaceCollector = scCollector1;
    
    offsetSurfaceBuilder1.PartialOption = true;
    
    NXOpen.Session.UndoMarkId markId2;
    markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "偏置曲面");
    
    theSession.DeleteUndoMark(markId2, null);
    
    NXOpen.Session.UndoMarkId markId3;
    markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "偏置曲面");
    
    NXOpen.NXObject nXObject1;
    nXObject1 = offsetSurfaceBuilder1.Commit();
    
    theSession.DeleteUndoMark(markId3, null);
    
    theSession.SetUndoMarkName(markId1, "偏置曲面");
    
    workPart.Expressions.Delete(expression1);
    
    NXOpen.Expression expression2 = offsetSurfaceBuilder1.Radius;
    NXOpen.Expression expression3 = faceSetOffset1.Offset;
    offsetSurfaceBuilder1.Destroy();
    
    // ----------------------------------------------
    //   選單:工具(T)->操作記錄(J)->停止錄製(S)
    // ----------------------------------------------
    
  }
  public static int GetUnloadOption(string dummy) { return (int)NXOpen.Session.LibraryUnloadOption.Immediately; }
}

其實,獲得相切面的程式碼是在:

NXOpen.ScCollector scCollector1;
    scCollector1 = workPart.ScCollectors.CreateCollector();
    
    NXOpen.Features.SweepAlongGuide sweepAlongGuide1 = ((NXOpen.Features.SweepAlongGuide)workPart.Features.FindObject("SWEEP(2)"));
    NXOpen.Face face1 = ((NXOpen.Face)sweepAlongGuide1.FindObject("FACE 563448 {(-8.5786437626905,0,191.4213562373095) SWEEP(2)}"));
    NXOpen.Face[] boundaryFaces1 = new NXOpen.Face[0];
    NXOpen.FaceTangentRule faceTangentRule1;
    faceTangentRule1 = workPart.ScRuleFactory.CreateRuleFaceTangent(face1, boundaryFaces1, 0.050000000000000003);
    
    NXOpen.SelectionIntentRule[] rules1 = new NXOpen.SelectionIntentRule[1];
    rules1[0] = faceTangentRule1;
    scCollector1.ReplaceRules(rules1, false);
    

這裡獲得的是NXOpen.ScCollector scCollector1,如何將NXOpen.ScCollector scCollector1中的面提取出來,作為NXObject,裡面沒有涉及。經過研究,可以採用如下的函式實現:

/// <summary>
        /// 獲取面的相切面(tangentFaces)
        /// </summary>
        ///<returns>tangentFaces[]</returns>>
        public Face[] GetTangentFaces()
        {
            Face [] tangentFaces = new Face[0];
            NXOpen.FaceTangentRule faceTangentRule;
            NXOpen.Face[] boundaryFaces1 = new NXOpen.Face[0];
            faceTangentRule = workPart.ScRuleFactory.CreateRuleFaceTangent(theFace, boundaryFaces1, 0.5);

            NXOpen.SelectionIntentRule[] rules = new NXOpen.SelectionIntentRule[1];
            rules[0] = faceTangentRule;
            NXOpen.ScCollector scCollector;
            scCollector = workPart.ScCollectors.CreateCollector();
            scCollector.ReplaceRules(rules, false);
            TaggedObject[] taggedObjects = scCollector.GetObjects();
            foreach (var to in taggedObjects)
            {
                Array.Resize(ref tangentFaces, tangentFaces.Length + 1);
                tangentFaces[tangentFaces.Length - 1] = (Face)to;
            }
            return tangentFaces;
        }

採用TaggedObject[] taggedObjects = scCollector.GetObjects();來獲取面物件。

相關文章