NX Journal 円筒ボディを作成

2022/01/08 categories:NX Journal| tags:NX Journal|Python|

NX journalで円筒ボディを作成するジャーナルを作ってみました。

処理内容

処理内容は以下の通りです。

テストとして作成した下記のコードで、1個目のフィーチャーのデータム座標系のXY平面上に、X軸を水平方向としてスケッチを作成して、そのスケッチ上に(0, 0, 0)を中心として半径50mmの円を作成して、そのスケッチを100mm押し出します。

Pythonコード

import math
import NXOpen
import NXOpen.Features
import NXOpen.GeometricUtilities

def main():
    theSession  = NXOpen.Session.GetSession()
    workPart = theSession.Parts.Work

    # get datum
    datum_plane, datum_axis = get_datum_plane_and_axis_from_csys(workPart, 'XY', 'X')
    
    # create sketch
    sketch = create_sketch(workPart, datum_plane, datum_axis)

    # add arc in sketch
    sketch.Activate(NXOpen.Sketch.ViewReorient.TrueValue)
    create_circle_in_avtive_sketch(theSession, workPart, 0.0, 0.0, 0.0, 50.0)
    theSession.ActiveSketch.Update()
    theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.TrueValue, NXOpen.Sketch.UpdateLevel.Model)

    # extrude sketch
    extrude(workPart, sketch, 0.0, 100.0, NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create, [NXOpen.Body.Null])

def get_datum_plane_and_axis_from_csys(part, plane_name, axis_name):
    datum_plane, datum_axis = None, None
    for feature in part.Features:
        if not type(feature) is NXOpen.Features.DatumCsys:
            return None, None
        for entity in feature.GetEntities():
            if type(entity) is NXOpen.DatumPlane:
                if getattr(entity.Normal, plane_name[0]) == 0 and getattr(entity.Normal, plane_name[1]) == 0:
                    datum_plane = entity
            if type(entity) is NXOpen.DatumAxis:
                if getattr(entity.Direction, axis_name) == 1:
                    datum_axis = entity
        return datum_plane, datum_axis

def create_sketch(part, datum_plane, datum_axis):
    plane1 = part.Planes.CreatePlane(NXOpen.Point3d(0.0, 0.0, 0.0), datum_plane.Normal, NXOpen.SmartObject.UpdateOption.WithinModeling)
    plane1.SetMethod(NXOpen.PlaneTypes.MethodType.Distance)
    plane1.SetGeometry([datum_plane])
    plane1.SetFlip(False)
    plane1.SetReverseSide(False)
    plane1.SetAlternate(NXOpen.PlaneTypes.AlternateType.One)
    plane1.Evaluate()

    sketchInPlaceBuilder1 = part.Sketches.CreateSketchInPlaceBuilder2(NXOpen.Sketch.Null)
    sketchInPlaceBuilder1.PlaneReference = plane1
    sketchInPlaceBuilder1.PlaneOption = NXOpen.Sketch.PlaneOption.ExistingPlane
    sketchInPlaceBuilder1.AxisReference = part.Directions.CreateDirection(datum_axis, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling)
    sketchInPlaceBuilder1.SketchOrigin = part.Points.CreatePoint( NXOpen.Point3d(0.0, 0.0, 0.0) )
    sketch1 = sketchInPlaceBuilder1.Commit()
    sketchInPlaceBuilder1.Destroy()
    return sketch1

def create_circle_in_avtive_sketch(theSession, part, x, y, z, r):
    nXMatrix1 = theSession.ActiveSketch.Orientation
    arc1 = part.Curves.CreateArc(NXOpen.Point3d(x, y, z), nXMatrix1, r, 0.0, ( 360.0 * math.pi/180.0 ))
    theSession.ActiveSketch.AddGeometry(arc1, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints)

    sketchAutoDimensionBuilder1 = part.Sketches.CreateAutoDimensionBuilder()
    sketchAutoDimensionBuilder1.DimensionType = NXOpen.SketchAutoDimensionBuilder.DimType.Driving
    sketchAutoDimensionBuilder1.SelectionObject.Add(arc1)
    sketchAutoDimensionBuilder1.SetRules([
        NXOpen.Sketch.AutoDimensioningRule.Symmetric,
        NXOpen.Sketch.AutoDimensioningRule.AdjacentAngle,
        NXOpen.Sketch.AutoDimensioningRule.Length,
        NXOpen.Sketch.AutoDimensioningRule.HorizontalVertical,
        NXOpen.Sketch.AutoDimensioningRule.ReferenceAxes
    ])
    nXObject1 = sketchAutoDimensionBuilder1.Commit()
    sketchAutoDimensionBuilder1.Destroy()
    return nXObject1

def extrude(part, sketch, start_value, end_value, boolean_type, target_bodies):
    extrudeBuilder1 = part.Features.CreateExtrudeBuilder(NXOpen.Features.Feature.Null)
    extrudeBuilder1.Section = part.Sections.CreateSection(0.0095, 0.01, 0.5)
    extrudeBuilder1.Section.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves)
    extrudeBuilder1.Section.AllowSelfIntersection(True)
    extrudeBuilder1.Section.AddToSection(
        [ part.ScRuleFactory.CreateRuleCurveFeature([sketch.Feature]) ], 
        NXOpen.NXObject.Null, NXOpen.NXObject.Null, NXOpen.NXObject.Null, 
        NXOpen.Point3d(0.0, 0.0, 0.0), NXOpen.Section.Mode.Create, False
    )
    extrudeBuilder1.AllowSelfIntersectingSection(True)
    extrudeBuilder1.DistanceTolerance = 0.01
    extrudeBuilder1.BooleanOperation.Type = boolean_type
    extrudeBuilder1.BooleanOperation.SetTargetBodies(target_bodies)
    extrudeBuilder1.Limits.StartExtend.Value.RightHandSide = str(start_value)
    extrudeBuilder1.Limits.EndExtend.Value.RightHandSide = str(end_value)
    extrudeBuilder1.SmartVolumeProfile.OpenProfileSmartVolumeOption = False
    extrudeBuilder1.SmartVolumeProfile.CloseProfileRule = NXOpen.GeometricUtilities.SmartVolumeProfileBuilder.CloseProfileRuleType.Fci
    extrudeBuilder1.Direction = part.Directions.CreateDirection(sketch, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling)
    extrudeBuilder1.ParentFeatureInternal = True
    feature1 = extrudeBuilder1.CommitFeature()
    extrudeBuilder1.Destroy()
    return feature1

if __name__ == '__main__':
    main()

Share post

Related Posts

Comments

comments powered by Disqus