|
| 1 | +import os |
| 2 | +import math |
| 3 | +from typing import List, Optional, cast |
| 4 | + |
| 5 | +import NXOpen |
| 6 | +import NXOpen.Features |
| 7 | +import NXOpen.GeometricUtilities |
| 8 | +import NXOpen.Assemblies |
| 9 | + |
| 10 | +the_session: NXOpen.Session = NXOpen.Session.GetSession() |
| 11 | +the_lw: NXOpen.ListingWindow = the_session.ListingWindow |
| 12 | + |
| 13 | + |
| 14 | +def get_all_bodies_in_component(component: NXOpen.Assemblies.Component) -> List[NXOpen.Body]: |
| 15 | + """ |
| 16 | + Get all the bodies in the given component. (thus not the part) |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + component : NXOpen.Assemblies.Component |
| 21 | + The component for which to get all bodies. |
| 22 | + |
| 23 | + Returns |
| 24 | + ------- |
| 25 | + List[NXOpen.Body] |
| 26 | + A list of all the bodies in the component. |
| 27 | +
|
| 28 | + NOTES |
| 29 | + ----- |
| 30 | + The bodies in the component are not the same as the bodies in the part. As a part be be used multiple times in an assembly. |
| 31 | + Tested in Simcenter 2312 |
| 32 | + """ |
| 33 | + if component is None: |
| 34 | + return [] |
| 35 | + all_bodies_in_part: List[NXOpen.Body] = [body for body in component.Prototype.Bodies] |
| 36 | + all_bodies_in_component: List[NXOpen.Body] = [component.FindOccurrence(body) for body in all_bodies_in_part] |
| 37 | + if all_bodies_in_component is None or all_bodies_in_component == [None] * len(all_bodies_in_component): |
| 38 | + # the component doesn't contain any bodies, so we return an empty list |
| 39 | + # this happens for example also when the 'reference set' is set to 'MODEL' |
| 40 | + return [] |
| 41 | + return all_bodies_in_component |
| 42 | + |
| 43 | + |
| 44 | +def get_all_curves_in_component(component: NXOpen.Assemblies.Component) -> List[NXOpen.Curve]: |
| 45 | + """ |
| 46 | + Get all the curves in the given component. (thus not the part) |
| 47 | +
|
| 48 | + Parameters |
| 49 | + ---------- |
| 50 | + component : NXOpen.Assemblies.Component |
| 51 | + The component for which to get all curves. |
| 52 | + |
| 53 | + Returns |
| 54 | + ------- |
| 55 | + List[NXOpen.Curve] |
| 56 | + A list of all the curves in the component. |
| 57 | +
|
| 58 | + NOTES |
| 59 | + ----- |
| 60 | + The curves in the component are not the same as the curves in the part. As a part be be used multiple times in an assembly. |
| 61 | + Tested in Simcenter 2312 |
| 62 | + """ |
| 63 | + if component is None: |
| 64 | + return [] |
| 65 | + all_curves_in_part: List[NXOpen.Curve] = [curve for curve in component.Prototype.Curves] |
| 66 | + the_lw.WriteFullline(f'Found {len(all_curves_in_part)} curves in component {component.JournalIdentifier}') |
| 67 | + all_curves_in_component: List[NXOpen.Curve] = [component.FindOccurrence(curve) for curve in all_curves_in_part] |
| 68 | + if all_curves_in_component is None or all_curves_in_component == [None] * len(all_curves_in_part): |
| 69 | + # the component doesn't contain any curves, so we return an empty list |
| 70 | + # this happens for example also when the reference set is set to 'MODEL' |
| 71 | + return [] |
| 72 | + return all_curves_in_component |
| 73 | + |
| 74 | + |
| 75 | +def get_all_points_in_component(component: NXOpen.Assemblies.Component) -> List[NXOpen.Point]: |
| 76 | + """ |
| 77 | + Get all the points in the given component. (thus not the part) |
| 78 | +
|
| 79 | + Parameters |
| 80 | + ---------- |
| 81 | + component : NXOpen.Assemblies.Component |
| 82 | + The component for which to get all points. |
| 83 | + |
| 84 | + Returns |
| 85 | + ------- |
| 86 | + List[NXOpen.Point] |
| 87 | + A list of all the points in the component. |
| 88 | +
|
| 89 | + NOTES |
| 90 | + ----- |
| 91 | + The points in the component are not the same as the points in the part. As a part be be used multiple times in an assembly. |
| 92 | + Tested in Simcenter 2312 |
| 93 | + """ |
| 94 | + if component is None: |
| 95 | + return [] |
| 96 | + all_points_in_part: List[NXOpen.Point] = [point for point in component.Prototype.Points] |
| 97 | + all_points_in_component: List[NXOpen.Point] = [component.FindOccurrence(point) for point in all_points_in_part] |
| 98 | + if all_points_in_component is None or all_points_in_component == [None] * len(all_points_in_component): |
| 99 | + # the component doesn't contain any points, so we return an empty list |
| 100 | + # this happens for example also when the 'reference set' is set to 'MODEL' |
| 101 | + return [] |
| 102 | + return all_points_in_component |
| 103 | + |
| 104 | + |
| 105 | +def create_component_from_bodies(bodies: List[NXOpen.Body], |
| 106 | + component_name: str, |
| 107 | + delete_original_bodies:bool=True, |
| 108 | + add_defining_objects:bool=True, |
| 109 | + work_part: NXOpen.Part=None) -> NXOpen.Assemblies.Component: |
| 110 | + ''' |
| 111 | + Create a new component from a list of bodies. |
| 112 | +
|
| 113 | + Parameters |
| 114 | + ---------- |
| 115 | + bodies : List[NXOpen.Body] |
| 116 | + The bodies to be used in the new component. |
| 117 | + component_name : str |
| 118 | + The name of the new component. |
| 119 | + delete_original_bodies : bool, optional |
| 120 | + Whether to delete the original bodies. Defaults to True. |
| 121 | + add_defining_objects : bool, optional |
| 122 | + Whether to add the defining objects. Defaults to True. |
| 123 | + work_part : NXOpen.Part, optional |
| 124 | + The part to create the component in. Defaults to work part. |
| 125 | +
|
| 126 | + Returns |
| 127 | + ------- |
| 128 | + NXOpen.Assemblies.Component |
| 129 | + The new component. |
| 130 | +
|
| 131 | + NOTES |
| 132 | + ----- |
| 133 | + This is based on the GUI funcionality of creating a new component from a set of bodies. |
| 134 | + Tested in Simcenter 2406 |
| 135 | + ''' |
| 136 | + if work_part is None: |
| 137 | + work_part = the_session.Parts.Work |
| 138 | + |
| 139 | + file_new: NXOpen.FileNew = the_session.Parts.FileNew() |
| 140 | + file_new.UseBlankTemplate = False |
| 141 | + file_new.ApplicationName = "ModelTemplate" |
| 142 | + file_new.Units = NXOpen.Part.Units.Millimeters |
| 143 | + file_new.TemplateType = NXOpen.FileNewTemplateType.Item |
| 144 | + file_new.TemplatePresentationName = "Model" |
| 145 | + file_new.AllowTemplatePostPartCreationAction(False) |
| 146 | + file_new.TemplateFileName = "model-plain-1-mm-template.prt" |
| 147 | + file_new.NewFileName = os.path.join(os.path.dirname(work_part.FullPath), f'{component_name}.prt') |
| 148 | + if os.path.exists(file_new.NewFileName): |
| 149 | + os.remove(file_new.NewFileName) |
| 150 | + |
| 151 | + create_new_component_builder: NXOpen.Assemblies.CreateNewComponentBuilder = work_part.AssemblyManager.CreateNewComponentBuilder() |
| 152 | + create_new_component_builder.NewComponentName = component_name |
| 153 | + create_new_component_builder.ReferenceSetName = "MODEL" |
| 154 | + create_new_component_builder.OriginalObjectsDeleted = delete_original_bodies |
| 155 | + create_new_component_builder.DefiningObjectsAdded = add_defining_objects |
| 156 | + for body in bodies: |
| 157 | + create_new_component_builder.ObjectForNewComponent.Add(body) |
| 158 | + |
| 159 | + create_new_component_builder.NewFile = file_new |
| 160 | + component: NXOpen.Assemblies.Component = create_new_component_builder.Commit() |
| 161 | + create_new_component_builder.Destroy() |
| 162 | + |
| 163 | + return component |
| 164 | + |
0 commit comments