cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

[Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElement()

Highlighted
Participant

[Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElement()

Dear sir,
 
Now I am solving a design optimization problem using Mathcad prime 6.0 through python API. I uses comtypes library to deal with API programming in my Python code.
 
In python code, I can set a single variable easily and vice verse. However, I could not set a matrix and get it from Mathcad.
 
I really love to exchange variables which whether it is a real single variable or matrix between Python and Mathcad.
 
I could not find any solution method for this trouble.
 
Please help me with this issue. I really want to make them exchangeable for reproducible research work. It is not commercial but academic.
 
With my best regards,
 
JW
---------------------------------------------------------------------------------------
The following is the python code to call and set matrix in Mathcad. Two functions are defined. The first(set_element) is  to set the elements of the matrix in Mathcad but the second(get_element) is to get the element of the matrix from Mathcad.
 
---
   import comtypes.client as CC
   import comtypes
 
    def set_element(self, row_index, column_index, value):
        """ Sets the value of an element in the Matrix """
        if self.object is not None:
            try:
                row, col = int(row_index), int(column_index)
                self.object.SetMatrixElement(row, col, value)
            except ValueError:
                raise ValueError("Matrix maths can only use numerical values")
            except:
                raise Exception("COM Error setting element value")  # Hidden for above @FIXME
        else:
            raise TypeError("Matrix must first be created")

    def get_element(self, row_index, column_index):
        """ Fetches the value of an element in the Matrix """
        if self.object is not None:
            try:
                row, col = int(row_index), int(column_index)
                self.object.GetMatrixElement(row, col)
            except ValueError:
                raise ValueError("Matrix maths can only use numerical values")
            except:
                raise Exception("COM Error fetching element value")
        else:
            raise TypeError("Matrix must first be created")
 
 
5 REPLIES 5
Highlighted

Re: [Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElemen

I remember there were a few threads in this forum about exchanging matrices via the API in the past but I am not sure if there ever was a satisfying solution. You may try the search function in the forum to find those threads.

While not specifically covering python, maybe this post can be of help: https://community.ptc.com/t5/PTC-Mathcad/Using-Matrix-with-Prime-3-1-API/m-p/313195/highlight/true#M...

Highlighted

Re: [Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElemen

I really appreciate it.  I will look at it.

Highlighted

Re: [Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElemen

Is there anybody who can reach https://www.ptc.com/en/support/article/CS301830?

 

The name of article is 'Article - CS301830 How to set a matrix element value using API SetMatrixElement() and SetMatrixValue() in Mathcad Prime'.

 

It is the right thing for me.

 

Please help me look at the article.

 

JW

 

Highlighted

Re: [Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElemen

Here you are, but the "article" just consists of the code example which you can also find in the thread I linked to in my first answer.

------------------------------------------------------- snip -------------------------------------------------

How to set a matrix element value using API SetMatrixElement() and SetMatrixValue() in Mathcad Prime

Modified: 28-Feb-2019   

 

Applies To

  • Mathcad Prime 3.1 to 5.0
  • API

Description

  • How to set a matrix element value using API SetMatrixElement() and SetMatrixValue()

Resolution

Highlighted

Re: [Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElemen

It is ridiculous.  Even though I coded like the c++ in the above, it still does not work. I do not know where I start from.

--------------------

<Functions>

    def create_matrix(self, rows, columns):
        """ Creates a Mathcad matrix """
        try:
            rows, columns = int(rows), int(columns)
            self.shape = (rows, columns)
            self.object = self.__ws.CreateMatrix(rows, columns)
            return True
        except ValueError:
            raise ValueError("Matrix dimensions must be integers")
            return False
        except:
            raise Exception("COM Error creating Mathcad matrix")
            return False
 
    def set_element(self, row_index, column_index, value):
        """ Sets the value of an element in the Matrix """
        if self.object is not None:
            try:
                row, col = int(row_index), int(column_index)
                self.object.SetMatrixElement(row, col, value)  # @FIXME
            except ValueError:
                raise ValueError("Matrix maths can only use numerical values")
            except:
                raise Exception("COM Error setting element value")  # Hidden for above @FIXME
        else:
            raise TypeError("Matrix must first be created")
-----------------------
<Main>
    A = Matrix()
    A.create_matrix(2,2) 
    A.set_element(1,1,3.0)
--------------------
<Error messege>
Loading Mathcad
Traceback (most recent call last): File "//Mac/Home/Downloads/MathcadPy-master/MathcadPy/Application.py", line 361, in <module> A.set_element(1,1,3.0) File "//Mac/Home/Downloads/MathcadPy-master/MathcadPy/Application.py", line 312, in set_element self.object.SetMatrixElement(row, col, value) # @FIXME OSError: exception: access violation writing 0x0000000000000001
 
 

 

 

Announcements