-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintellisense.py
69 lines (53 loc) · 2.72 KB
/
intellisense.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# intellisense by theScriptingEngineer (www.theScriptingEngineer.com)
# NXOpen Python Reference Guide:
# https://docs.plm.automation.siemens.com/data_services/resources/nx/1899/nx_api/custom/en_US/nxopen_python_ref/index.html
# This file is used to demonstrate a working intellisense (code completion) for
# writing NXOpen journals using Python
import sys
import math
import NXOpen
import NXOpen.CAE
from typing import List, cast, Optional, Union
the_session: NXOpen.Session = NXOpen.Session.GetSession()
base_part = the_session.Parts.BaseWork
the_lw: NXOpen.ListingWindow = the_session.ListingWindow
the_uf_session: NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession()
assem = the_uf_session.Assem
def get_solution(solution_name: str) -> Union[NXOpen.CAE.SimSolution, None]:
"""This function returns the SimSolution object with the given name.
Returns None if not found, so the user can check and act accordingly
Parameters
----------
solution_name: int
The name of the solution to return, case insensitive
Returns
-------
NXOpen.CAE.SimSolution or None
The FIRST solution object with the given name if found, None otherwise
"""
# The next commented block is an extended example where each object is typed.
# sim_part: NXOpen.CAE.SimPart = cast(NXOpen.CAE.SimPart, basePart) # explicit casting makes it clear
# sim_simulation: NXOpen.CAE.SimSimulation = sim_part.Simulation
# sim_solution_collection: NXOpen.CAE.SimSolutionCollection = sim_simulation.Solutions
# sim_solutions: List[NXOpen.CAE.SimSolution] = sim_solution_collection.ToArray()
# sim_solution: List[NXOpen.CAE.SimSolution] = [item for item in sim_solutions if item.Name.lower == solution_name.lower()]
# # minimal working example PyCharm
# sim_part: NXOpen.CAE.SimPart = basePart # simply typing the sim_part provides intellisense.
# sim_solutions: List[NXOpen.CAE.SimSolution] = sim_part.Simulation.Solutions.ToArray()
# # minimal working example Visual Studio Code
sim_part = cast(NXOpen.CAE.SimPart, base_part) # explicit casting required in Visual Studio Code
sim_solutions = sim_part.Simulation.Solutions.ToArray()
sim_solution = [item for item in sim_solutions if item.Name.lower() == solution_name.lower()]
if len(sim_solution) == 0:
# solution with given name not found
return None
# return the first solution with the requested name
return sim_solution[0]
def main():
the_lw.Open()
the_lw.WriteFullline("Starting Main() in " + the_session.ExecutingJournal)
my_solution: NXOpen.CAE.SimSolution = get_solution("my_solution_name")
if my_solution is not None:
print(my_solution.JournalIdentifier)
if __name__ == '__main__':
main()