5050from ansys .dpf .core import __version__ , errors , server_context , server_factory
5151from ansys .dpf .core ._version import min_server_version , server_to_ansys_version
5252from ansys .dpf .core .check_version import server_meet_version
53+ from ansys .dpf .core .server_context import ServerContext
5354from ansys .dpf .gate import data_processing_grpcapi , load_api
5455
5556if TYPE_CHECKING : # pragma: no cover
@@ -114,7 +115,11 @@ def _verify_ansys_path_is_valid(ansys_path, executable, path_in_install=None):
114115
115116
116117def _run_launch_server_process (
117- ip , port , ansys_path = None , docker_config = server_factory .RunningDockerConfig ()
118+ ip ,
119+ port ,
120+ ansys_path = None ,
121+ docker_config = server_factory .RunningDockerConfig (),
122+ context : ServerContext = None ,
118123):
119124 bShell = False
120125 if docker_config .use_docker :
@@ -127,13 +132,17 @@ def _run_launch_server_process(
127132 if os .name == "nt" :
128133 executable = "Ans.Dpf.Grpc.bat"
129134 run_cmd = f"{ executable } --address { ip } --port { port } "
135+ if context is not None :
136+ run_cmd += f" --context { context .licensing_context_type } "
130137 else :
131138 executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover
132139 run_cmd = [
133140 executable ,
134141 f"--address { ip } " ,
135142 f"--port { port } " ,
136143 ] # pragma: no cover
144+ if context is not None :
145+ run_cmd .append (f"--context { context .licensing_context_type } " )
137146 path_in_install = load_api ._get_path_in_install (internal_folder = "bin" )
138147 dpf_run_dir = _verify_ansys_path_is_valid (ansys_path , executable , path_in_install )
139148
@@ -204,7 +213,9 @@ def read_stdout():
204213 raise RuntimeError (errstr )
205214
206215
207- def launch_dpf (ansys_path , ip = LOCALHOST , port = DPF_DEFAULT_PORT , timeout = 10 ):
216+ def launch_dpf (
217+ ansys_path , ip = LOCALHOST , port = DPF_DEFAULT_PORT , timeout = 10 , context : ServerContext = None
218+ ):
208219 """Launch Ansys DPF.
209220
210221 Parameters
@@ -222,9 +233,10 @@ def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10):
222233 Maximum number of seconds for the initialization attempt.
223234 The default is ``10``. Once the specified number of seconds
224235 passes, the connection fails.
225-
236+ context : , optional
237+ Context to apply to DPF server when launching it.
226238 """
227- process = _run_launch_server_process (ip , port , ansys_path )
239+ process = _run_launch_server_process (ip , port , ansys_path , context = context )
228240 lines = []
229241 current_errors = []
230242 _wait_and_check_server_connection (
@@ -766,7 +778,7 @@ def __init__(
766778 launch_server : bool = True ,
767779 docker_config : DockerConfig = RUNNING_DOCKER ,
768780 use_pypim : bool = True ,
769- context : server_context .AvailableServerContexts = server_context .SERVER_CONTEXT ,
781+ context : server_context .ServerContext = server_context .SERVER_CONTEXT ,
770782 ):
771783 # Load DPFClientAPI
772784 from ansys .dpf .core .misc import is_pypim_configured
@@ -808,7 +820,7 @@ def __init__(
808820 timeout = timeout ,
809821 )
810822 else :
811- launch_dpf (ansys_path , ip , port , timeout = timeout )
823+ launch_dpf (ansys_path , ip , port , timeout = timeout , context = context )
812824 self ._local_server = True
813825
814826 # store port and ip for later reference
@@ -820,15 +832,11 @@ def __init__(
820832 self ._create_shutdown_funcs ()
821833 self ._check_first_call (timeout = timeout - (time .time () - start_time )) # Pass remaining time
822834 if context :
823- if context == core . AvailableServerContexts . no_context :
824- self ._base_service .initialize ( )
835+ try :
836+ self ._base_service .initialize_with_context ( context )
825837 self ._context = context
826- else :
827- try :
828- self ._base_service .initialize_with_context (context )
829- self ._context = context
830- except errors .DpfVersionNotSupported :
831- pass
838+ except errors .DpfVersionNotSupported :
839+ pass
832840 self .set_as_global (as_global = as_global )
833841
834842 def _check_first_call (self , timeout : float ):
@@ -1201,7 +1209,7 @@ def __init__(
12011209 launch_server : bool = True ,
12021210 docker_config : DockerConfig = RUNNING_DOCKER ,
12031211 use_pypim : bool = True ,
1204- context : server_context .AvailableServerContexts = server_context .SERVER_CONTEXT ,
1212+ context : server_context .ServerContext = server_context .SERVER_CONTEXT ,
12051213 ):
12061214 """Start the DPF server."""
12071215 # Use ansys.grpc.dpf
@@ -1247,7 +1255,7 @@ def __init__(
12471255 timeout = timeout ,
12481256 )
12491257 else :
1250- launch_dpf (ansys_path , ip , port , timeout = timeout )
1258+ launch_dpf (ansys_path , ip , port , timeout = timeout , context = context )
12511259 self ._local_server = True
12521260 from ansys .dpf .core import misc , settings
12531261
@@ -1267,14 +1275,11 @@ def __init__(
12671275
12681276 check_ansys_grpc_dpf_version (self , timeout )
12691277 if context :
1270- if context == core .AvailableServerContexts .no_context :
1278+ try :
1279+ self ._base_service .initialize_with_context (context )
12711280 self ._context = context
1272- else :
1273- try :
1274- self ._base_service .initialize_with_context (context )
1275- self ._context = context
1276- except errors .DpfVersionNotSupported :
1277- pass
1281+ except errors .DpfVersionNotSupported :
1282+ pass
12781283 self .set_as_global (as_global = as_global )
12791284
12801285 def _create_shutdown_funcs (self ):
0 commit comments