diff --git a/Examples/ex0004.py b/Examples/ex0004.py new file mode 100644 index 0000000..0a8e2a3 --- /dev/null +++ b/Examples/ex0004.py @@ -0,0 +1,40 @@ +# ex0004.py +import NXOpen +import NXOpen.Features + +def yacht_builder(session=NXOpen.Session.GetSession(), + work_part=NXOpen.Session.GetSession().Parts.Work, + yacht_length=25000.0, + yacht_width=6000.0, + hull_depth=3000.0): + mark_id = session.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Parametric Yacht") + mm = work_part.UnitCollection.FindObject("MilliMeter") + + # Expressions for the hull's dimensions + work_part.Expressions.CreateSystemExpressionWithUnits(f"Yacht_Length={yacht_length}", mm) + work_part.Expressions.CreateSystemExpressionWithUnits(f"Yacht_Width={yacht_width}", mm) + work_part.Expressions.CreateSystemExpressionWithUnits(f"Hull_Depth={hull_depth}", mm) + + # Hull + hull_builder = work_part.Features.CreateBlockFeatureBuilder(NXOpen.Features.Feature.Null) + hull_builder.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths + hull_origin = NXOpen.Point3d(0.0, 0.0, 0.0) + hull_builder.SetOriginAndLengths(hull_origin, "Yacht_Length", "Yacht_Width", "Hull_Depth") + hull_feature = hull_builder.Commit() + hull_builder.Destroy() + + # Cabin + cabin_builder = work_part.Features.CreateBlockFeatureBuilder(NXOpen.Features.Feature.Null) + cabin_builder.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths + cabin_origin = NXOpen.Point3d(float(yacht_length) / 4, 1000.0, float(hull_depth)) + cabin_builder.SetOriginAndLengths(cabin_origin, "Yacht_Length / 2", "Yacht_Width - 2000", "Hull_Depth * 3/4") + cabin_feature = cabin_builder.Commit() + cabin_builder.Destroy() + + return hull_feature, cabin_feature, mark_id + +def main(): + yacht_builder() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/README.md b/README.md index e829005..2d8cb4c 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,65 @@ if __name__ == '__main__': This creates a cylinder with the specified height and diameter that you can edit manually from the model history or update programmatically. +### Example 4: Building a Simple Parametric Yacht + +This example combines two `BlockFeatureBuilder` features with NX +expressions to build a simple two-block "yacht": a hull, and a smaller +cabin sitting centered on top of it. Unlike the cylinder in Example 3, +the cabin's dimensions are written as formulas that reference the hull's +own expressions, so resizing the hull updates the cabin proportionally. + +```python +# ex0004.py +import NXOpen +import NXOpen.Features + +def yacht_builder(session=NXOpen.Session.GetSession(), + work_part=NXOpen.Session.GetSession().Parts.Work, + yacht_length=25000.0, + yacht_width=6000.0, + hull_depth=3000.0): + mark_id = session.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Parametric Yacht") + mm = work_part.UnitCollection.FindObject("MilliMeter") + + # Expressions for the hull's dimensions + work_part.Expressions.CreateSystemExpressionWithUnits(f"Yacht_Length={yacht_length}", mm) + work_part.Expressions.CreateSystemExpressionWithUnits(f"Yacht_Width={yacht_width}", mm) + work_part.Expressions.CreateSystemExpressionWithUnits(f"Hull_Depth={hull_depth}", mm) + + # Hull + hull_builder = work_part.Features.CreateBlockFeatureBuilder(NXOpen.Features.Feature.Null) + hull_builder.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths + hull_origin = NXOpen.Point3d(0.0, 0.0, 0.0) + hull_builder.SetOriginAndLengths(hull_origin, "Yacht_Length", "Yacht_Width", "Hull_Depth") + hull_feature = hull_builder.Commit() + hull_builder.Destroy() + + # Cabin + cabin_builder = work_part.Features.CreateBlockFeatureBuilder(NXOpen.Features.Feature.Null) + cabin_builder.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths + cabin_origin = NXOpen.Point3d(float(yacht_length) / 4, 1000.0, float(hull_depth)) + cabin_builder.SetOriginAndLengths(cabin_origin, "Yacht_Length / 2", "Yacht_Width - 2000", "Hull_Depth * 3/4") + cabin_feature = cabin_builder.Commit() + cabin_builder.Destroy() + + return hull_feature, cabin_feature, mark_id + +def main(): + yacht_builder() + +if __name__ == '__main__': + main() +``` + +This creates two blocks driven by three named NX expressions +(`Yacht_Length`, `Yacht_Width`, `Hull_Depth`): a hull, and a cabin whose +edge lengths are formulas referencing those same expressions (e.g. +`"Yacht_Length / 2"`). Edit any of the three expressions from the NX +Expressions dialog and both blocks resize together. + +Each builder is also explicitly destroyed with `Destroy()` after `Commit()`, which releases the builder's resources. This a good habit once a script starts creating more than one feature. + ## Repository Structure ``` @@ -158,7 +217,8 @@ NXOpen_Python_tutorials/ ├── Examples/ │ ├── ex0001.py # Message box "Hello World" │ ├── ex0002.py # Listing window output -│ └── ex0003.py # Cylinder feature creation +│ ├── ex0003.py # Cylinder feature creation +│ └── ex0004.py # Parametric modeling ├── Pictures/ # Screenshots for documentation ├── .gitignore ├── LICENSE # CC0-1.0 Public Domain @@ -185,7 +245,13 @@ gitGraph commit id: "readme-paths" checkout main merge contributor id: "PR#3" - commit id: "grammar-fixes" tag: "current" + commit id: "grammar-fixes" + branch parametric-example + checkout parametric-example + commit id: "add-ex0004" + commit id: "update-readme" + checkout main + merge parametric-example id: "PR#4" tag: "current" ``` ### Timeline @@ -205,6 +271,11 @@ gitGraph - Grammar and capitalization fixes - Improved documentation clarity and formatting +**July 2026: Parametric Modeling** +- Created the parametric-example branch +- Added ex0004.py for parametric example creation +- Updated the repository history and timeline documentation + ## Resources ### Official Documentation