


{"id":303841,"date":"2023-08-29T11:27:19","date_gmt":"2023-08-29T11:27:19","guid":{"rendered":"\/forum\/forums\/topic\/python-code-object-not-working-when-updating-design-points\/"},"modified":"2023-09-04T10:27:48","modified_gmt":"2023-09-04T10:27:48","slug":"python-code-object-not-working-when-updating-design-points","status":"closed","type":"topic","link":"https:\/\/innovationspace.ansys.com\/forum\/forums\/topic\/python-code-object-not-working-when-updating-design-points\/","title":{"rendered":"Python Code Object not working when updating Design Points"},"content":{"rendered":"<p>Hello everyone!<\/p>\n<p>&nbsp;<\/p>\n<p>I am performing a parameter study in Transient Thermal in which I need to export the temperature results for all time steps to a CSV file. I have a Python script which works perfectly fine when I run it in the Mechanical Scripting editor. But when I try to update the Design Points from Workbench, the Python Code does not work.<\/p>\n<p>&nbsp;<\/p>\n<p>For reference, this is script I am using at the moment which runs perfectly fine in Mechanical Scripting editor:<br \/>&nbsp; &nbsp;&nbsp;<br \/>&nbsp; &nbsp; import csv<br \/>&nbsp; &nbsp; import os<br \/>&nbsp; &nbsp; import wbjn<br \/>&nbsp; &nbsp;&nbsp;<br \/>&nbsp;&nbsp; <br \/>&nbsp; &nbsp; dpn=wbjn.ExecuteCommand(ExtAPI,&#8221;returnValue(a+Parameters.GetActiveDesignPoint().Name)&#8221;,a=&#8221;DP&#8221;)<br \/>&nbsp; &nbsp;&nbsp;<br \/>&nbsp; &nbsp; cmd = &#8220;returnValue(GetUserFilesDirectory())&#8221;<br \/>&nbsp; &nbsp; user_dir = wbjn.ExecuteCommand(ExtAPI, cmd)<br \/>&nbsp; &nbsp;&nbsp;<br \/>&nbsp; &nbsp;&nbsp;<\/p>\n<p>&nbsp; &nbsp; def writeCSV(filename, data):<\/p>\n<p>&nbsp; &nbsp;# Function to write python list to a csv file<br \/>&nbsp; &nbsp; &nbsp; &nbsp; with open(filename, &#8220;wb&#8221;) as csvfile:<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spamwriter = csv.writer(csvfile, delimiter=&#8217;;&#8217;,<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quotechar=&#8217;|&#8217;, quoting=csv.QUOTE_MINIMAL)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for row in data:<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spamwriter.writerow(row)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp; &nbsp; ResultsOfInterest = []<\/p>\n<p>&nbsp; &nbsp; ResultsOfInterest.append(&#8216;Directional Deformation&#8217;)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp; &nbsp; #import wbjn<\/p>\n<p>&nbsp; &nbsp; AnalysisNumber=0<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp; &nbsp; solution=Model.Analyses[AnalysisNumber].Solution<\/p>\n<p>&nbsp; &nbsp; for j, item in enumerate(solution.Children):<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; if item.GetType() == Ansys.ACT.Automation.Mechanical.Results.DeformationResults.DirectionalDeformation:<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item.Name in ResultsOfInterest:<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.Activate()<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data=[]<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; del data[:]<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Pane=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Con = Pane.ControlUnknown<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for R in range(1,Con.RowsCount+1):<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.append([])<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for C in range(2,Con.ColumnsCount+1):<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[-1].append(Con.cell(R,C).Text)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeCSV(user_dir + &#8220;\/&#8221; + dpn+ &#8220;Deformation.csv&#8221;, data)<\/p>\n<p>&nbsp;<\/p>\n<p>As I stated earlier, while updating the Design Points through Parameter Set in Workbench, the results do not get stored. Is there any solution for this?<\/p>\n<p>&nbsp;<\/p>\n<p>P.S.: I have already checked the option where one needs to connect the Python script under Workbench -&gt; Tools -&gt; Option -&gt; Mechanical -&gt; Connect\/Run Python Code Objects when Mechanical is launched.<\/p>\n<p>&nbsp;<\/p>\n<p>EDIT: The above script is for a sample problem in which I am trying to obtain results before I actually implement it for my use-case. Under Ansys.ACT.Automation.Mechanical.Results, results of choice can be extracted.&nbsp;<\/p>\n","protected":false},"template":"","class_list":["post-303841","topic","type-topic","status-closed","hentry","topic-tag-python-ironpython-1","topic-tag-ansys-transient-thermal-2","topic-tag-design-of-experiements-2","topic-tag-doe-1","topic-tag-excel-csv","topic-tag-export-result","topic-tag-mechanical","topic-tag-transient-thermal"],"aioseo_notices":[],"acf":[],"custom_fields":[{"0":{"_bbp_subscription":["292401","18229","170445"],"_bbp_author_ip":["23.206.193.146"]," _bbp_last_reply_id":["0"]," _bbp_likes_count":["0"],"_btv_view_count":["3634"],"_edit_lock":["1693308523:229242"],"_bbp_status":["publish"],"_bbp_topic_status":["unanswered"],"_bbp_likes_count":["1"],"_bbp_topic_id":["303841"],"_bbp_forum_id":["27791"],"_bbp_engagement":["18229","170445","292401"],"_bbp_voice_count":["3"],"_bbp_reply_count":["15"],"_bbp_last_reply_id":["304777"],"_bbp_last_active_id":["304777"],"_bbp_last_active_time":["2023-09-04 08:36:41"]},"test":"akshay-panchwaghrwth-aachen-de"}],"_links":{"self":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/topics\/303841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/topics"}],"about":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/topics\/303841\/revisions"}],"wp:attachment":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/media?parent=303841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}