I'm trying to write a python script which will import a number of different step files into abaqus. I've been trying to use the.PartFromGeometryFile() command but every time i try to use it i keep getting an attribute error.
This is the code that i've written.
Code to import step file
This is the error message.
Error Message
The main issue is that i know that the attribute should exist as its included in the Abaqus scripting reference manual here https://classes.engineering.wustl.edu/2009/spring/mase5513/abaqus/docs/v6.6/books/ker/default.htm?startat=pt01ch33pyo03.html
I've tried creating a part object and then importing the geometry and accessing multiple different modules within abaqus but no matter what i've tried i get the same or a similar error.
I have been able to import the parts using the GUI but i need to study a lot of different parts so i need to be able to automate this process.
I feel there may be a simple fix that i am missing but I have very limited experience coding in general and very little writing scripts for abaqus.
Any help would be greatly appreciated.
Related
I'm working on Abaqus 6.14 plugin that would help me in my Engineer's Thesis, which I'm writing in Python. According to Abaqus scripting reference guide it is possible to import materials from Output Databases (*.odb files) by calling:
from abaqus import mdb
mdb.models[name].materialsFromOdb(filename)
However, as Abaqus allows user to export/import materials to/from relatively lightweight Material Libraries (*.lib files) and share them between models I would like to import the materials from these rather than from, often bulky, *.odb files.
Of course this can be done manually with ease, although I want to reduce amount of repetitive job with my plugin, as I need to run dozens of simulations on pretty similiar models but with varying materials and some other parameters. I am aware I could also provide necessary materials in template *.cae file too, although this could be quite inconvenient if I had to manually import new material(s) to dozens of models or I had to update existing material's properties.
What am I looking for is a workaround allowing to import materials from material libraries to Abaqus mdb models with Python scripts, but avoiding implementing custom *.lib files parser, if only such workaround exists.
The first thing you need to know is that Abaqus material library is just a pickled file. No special parser is needed to work with it. You can just use the standard Python libraries pickle or cPickle. Of course, you need to figure out the exact structure of the objects inside. This is not difficult as you'll see it's a just list of simple tuples.
However, if you have an existing material library and you want to import a material in your Abaqus CAE database, there is an existing method to do it.
There is a method in Abaqus which uses a material string from a material database and creates a material object from that. I can't remember the exact name, but if you import one material manually and look into abaqus.rpy file, you will see it inside.
One tricky thing here is that in order to use this method, you need to have a material string from the material library. This can be done by reading the material database file. As already mentioned, this is in pickle format.
As you know, Abaqus already has a way of reading data from the material library and importing it into a CAE model. They have a Python module which you could use, but it can only be used inside a GUI process, not the kernel one. If you want to spend some time, you can figure out which module does that. Inside Abaqus installation folder, you will find some .pyc files. If you use a Python decompiler, you can obtain source code for those modules. Look for those beginning with mat.
When using app = win32com.client.Dispatch('Some.Application'), is there any feasible way get code-completion in PyCharm? It is rather tedious having to retype (or copy-paste) everything from an API documentation, so would creating skeletons be. Is there no other way to let PyCharm know about the Interface provided via COM, especially if I can provide a .tlb file? Or is there at least some way automatically generate such a skeleton (or a wrapping module?) from the TypeLib?
Since there is no way for PyCharm to know the runtime type of app, you shouldn't expect to get code completion on app directly; at least not until they decide to add built-in support for generating code from type libraries.
However, you can exploit the fact that win32com implicitly generates code based on the type library as described in the first part of this answer, together with PyCharm's support for type hinting, to get code completion on COM methods.
Make sure that the Python types have been generated; their location is determined by the GUID of the COM object. For example, the types for Microsoft Word 2016 on my machine are available in
C:\Users\username\appdata\local\temp\gen_py\3.6\00020905-0000-0000-c000-000000000046x0x8x7\.
Add this folder to the path of your PyCharm Python interpreter; see e.g. this answer.
Import the modules for which you want code completion.
In the screenshots below, we use this approach with Word's Find:
Now, besides feeling dirty, this approach relies on the relevant types having been generated and the code completion is limited to the methods published by the object, so I imagine its usefulness in practice might be somewhat limited; in particular, anybody working on the code will have to generate the code, or the annotations will cause NameErrors. Personally, I would probably prefer using Jupyter for the exploratory part of the implementation process, and with minimal tweaks outlined in the answer mentioned above, Jupyter can be extended to have full code completion with win32com.
I have a Python code that needs to be able to execute a C++ code. I'm new to the idea of creating libraries but from what I have learned so far I need to know whether I need to use static or dynamic linking.
I have read up on the pros and cons of both but there is a lot of jargon thrown around that I do not understand yet and since I need to do this ASAP I was wondering if some light can be shed on this from somebody who can explain it simply to me.
So here's the situation. My C++ code generates some text files that have data. My Python code then uses those text files to plot the data. As a starter, I need to be able to run the C++ code directly from Python. Is DLL more suitable than SL? Or am I barking up the completely wrong tree?
Extra: is it possible to edit variables in my C++ code, compile it and execute it, all directly from Python?
It depends on your desired deployment. If you use dynamic linking will need to carefully manage the libraries (.so, .dll) on your path and ensure that the correct version is loaded. This can be helped if you include the version number in the filename, but then that has its own problems (security... displaying version numbers of your code is a bad idea).
Another benefit is that you can swap your library functionality without a re-compile as long as the interface does not change.
Statically linking is conceptually simpler and practically simpler. You only have to deploy one artefact (an .exe for example). I recommend you start with that until you need to move to the more complicated shared library setup.
Edit: I don't understand your "extra credit" question. What do you mean by "edit values"? If you mean can you modify variables that were declared in your C++ code, then yes you can as long as you use part of the public interface to do it.
BTW this advice is for the general decision. If you are linking from Python to C/C++ I think you need to use a shared library. Not sure as I haven't done it myself.
EDIT: To expand on "public interface". When you create a C++ library of whatever kind, you specify what functions are available to outside classes (look up how to to that). This is what I mean by public interface. Parts of your library are inaccessible but others (that you specify) are able to be called from client code (i.e. your python script). This allows you to modify the values that are stored in memory.
If you DO mean that you want to edit the actual C++ code from within your python I would suggest that you should re-design your application. You should be able to customise the run-time behaviour of your C++ library by providing the appropriate configuration.
If you give a solid example of what you mean by that we'll be able to give you better advice.
Yes it is possible!!
Try exploring subprocess module in python.
Following can be an example implementation of your scenario:
yourfile.cpp
#compilation
args = ['g++','-o','your_executable_name_with_path','yourfile.cpp_with_path']
your_compile = subprocess.Popen(args,stdin=subprocess.PIPE,stderr=subprocess.PIPE,stdout=subprocess.PIPE)
output,compilation_error = your_compile.communicate()
your_compile.wait()
#successful compilation then there will be execuatble
if not compilation_error:
#execuation
args = ['your_executable_name_with_path'] #command to run a an execuatble
your_run = subprocess.Popen(args,stdin=subprocess.PIPE,stderr=subprocess.PIPE,stdout=subprocess.PIPE)
your_code_output,runtime_error = your_run.communicate()
your_run.wait()
Further, you can tackle more cases and come up with an efficient design
I'm not quite sure how the idea of linking comes into what you are asking, but it sounds to me like you want to use something like SWIG, which allows you to create wrappers around C++ functions (and many other languages) which you can then call directly from your Python code.
Extra: is it possible to edit values in my C++ code, compile it and execute it directly from Python?
If I'm understanding this correctly, you want to use Python to change your C++ code, then compile and execute it? If this is the case, you may want to look into embedding the Python interpreter in your C++ program. This would mean doing things the other way around and having C++ run your Python script, instead of trying to do everything from Python.
One of the tools I use at work is Matlab, however due to server license there is limited number of users that can use it at the same time.
I decided to write a short script that will open Matlab - simple script with a infinite loop.
Now I want to improve my code a bit, to determine if the Matlab is actually opened (otherwise Licence error pops up).
Easy way would be just to check the process in task manager - unfortunately if error occurs as Matlab.exe process (the same as I would be in case of properly opened program).
So I figured out maybe it would be possible to check the name of the window header to determine if there is error or no. I tried to find some solution on the internet, with no luck. Could You provide me with some hint? Or maybe some other solution to the problem?
You can check with
$MATLABROOT/etc/lmstat -c yourlicencefile -a
and parse its output to see if you allocated a license or not to your computer.
I work in analyzing binary files, using Python. I have been using debuggers to do a dynamic analysis (i.e running the application and using breakpoints to get runtime execution). however, results can be improved if i can use some binary instrumentation fremework like PIN. The PIN is developed in C++ and provided as closed source (only dlls). We write something called PinTools do describe where and what we want to intercepts. I want to port PIN functionality into Python so that i continue using Python. I am aware of "ctypes" and boost-python.
My problem is: in order to use PIN, we write a pintool and run our bibnary executable with Pin and pintool (it is like running application with JIT). Now, I have no idea if I can use ctypes etc. to import PIN functions and use this python code for dynamically analyzing the binary. Can you please provide some suggestions or guidelines on how to proceed with this task.
So, in nut-n-shell, I want to create a Python interface (wrapper) to PIN framework.
Check out the ProcessTap project. Appears to implement exactly what you are looking for: http://code.google.com/p/processtap/
I was thinking about this recently, while I haven't looked into it, I would approach the problem like this: write a pintool that, upon initialization, starts an embedded python interpreter and imports a python module. I'd look at using SWIG to generate bindings for all the PIN api calls you want to use. Then the pintool would call a hardcoded function in the imported python module that would issue calls to the api to register more functions and do whatever you want to do.
I'm not sure how the callbacks would work, I don't know enough about SWIG. Also, this may fail if the program you're trying to instrument itself uses Python. But that's how I'd try to solve this problem to start out.