-1

I have some code which reads a step file and returns a shape (geometrical information). This uses the XDE of OCCD and will also be able to extract the attribute information such as colour or name or layer. But im new to pythonocc and im getting the following error :

'function' object has no attribute 'NewDocument' type is: AttributeError Traceback (most recent call last): File "d:\ShareUbuntu\Optimization\Optimization_NACA.py", line 117, in funcSU2 config.m1=ExtractTopo(config.meshDir,config.modelName1) File "d:\ShareUbuntu\Optimization\Optimization_NACA.py", line 463, in init self.main() File "d:\ShareUbuntu\Optimization\Optimization_NACA.py", line 469, in main [self.topo,colour_tool]=OCCB.read_step_file_with_attribute2(self.path+self.model+".stp") File "d:\ShareUbuntu\Optimization\OCCD_Basic.py", line 875, in read_step_file_with_attribute2 app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),h_doc) AttributeError: 'function' object has no attribute 'NewDocument' Traceback (most recent call last): File "d:\ShareUbuntu\Optimization\Optimization_NACA.py", line 1992, in main(config) File "d:\ShareUbuntu\Optimization\Optimization_NACA.py", line 64, in main res = fmin_slsqp(funcSU2, x0, f_eqcons=None, f_ieqcons=inequality_const_thickness,bounds=config.parmBounds, fprime=gradSU2, fprime_eqcons=None,fprime_ieqcons=inequality_const_thickness_grad, args=(config,), acc=1e-2,iprint=2, disp=None, full_output=1) File "C:\Users\yesbo.conda\envs\py39env\lib\site-packages\scipy\optimize_slsqp_py.py", line 206, in fmin_slsqp res = _minimize_slsqp(func, x0, args, jac=fprime, bounds=bounds, File "C:\Users\yesbo.conda\envs\py39env\lib\site-packages\scipy\optimize_slsqp_py.py", line 374, in _minimize_slsqp sf = _prepare_scalar_function(func, x, jac=jac, args=args, epsilon=eps, File "C:\Users\yesbo.conda\envs\py39env\lib\site-packages\scipy\optimize_optimize.py", line 383, in _prepare_scalar_function sf = ScalarFunction(fun, x0, args, grad, hess, File "C:\Users\yesbo.conda\envs\py39env\lib\site-packages\scipy\optimize_differentiable_functions.py", line 158, in init self._update_fun() File "C:\Users\yesbo.conda\envs\py39env\lib\site-packages\scipy\optimize_differentiable_functions.py", line 251, in _update_fun self._update_fun_impl() File "C:\Users\yesbo.conda\envs\py39env\lib\site-packages\scipy\optimize_differentiable_functions.py", line 155, in update_fun self.f = fun_wrapped(self.x) File "C:\Users\yesbo.conda\envs\py39env\lib\site-packages\scipy\optimize_differentiable_functions.py", line 137, in fun_wrapped fx = fun(np.copy(x), args) File "d:\ShareUbuntu\Optimization\Optimization_NACA.py", line 144, in funcSU2 return objconfig.functionScaling UnboundLocalError: local variable 'obj' referenced before assignment

Some of my code is as follows. I get the error after this line app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),h_doc)

    
    h_doc = TDocStd_Document
    #print "Empty Doc?", h_doc.IsNull()
   
    ### Create the application
    app = XCAFApp_Application.GetApplication
    print('mohsin before')
    app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),h_doc)

I tried to access the pythonocc documentation for different versions of pythonocc but i did not find. I think this code is written for some other version of pythonocc whereas i am using pythonocc 7.4.1.

  • 3
    Welcome to Stack Overflow. Please include your traceback as text and not as an image. Please also include more context in your code. What is ```TDocStd_Document```? Also, iiuc, you should do ```app = XCAFApp_Application.GetApplication()``` assuming that is your original intention. – ewokx Aug 24 '23 at 09:11
  • There is no correlation between the stack trace and the code in this question. The bug is in Optimization_NACA.py – DarkKnight Aug 24 '23 at 09:13
  • [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – Mark Seemann Aug 24 '23 at 09:16
  • Ok thanks next time, I will not post images. Thanks for letting me know. – Mohsin Bashir Aug 24 '23 at 09:27
  • the suggestion is to edit your question and fix it this time, not wait until next time – Anentropic Aug 24 '23 at 09:39
  • I think the bug is in the code because i tried importing the code directly to python interpreter and i still get this error "'function' object has no attribute 'NewDocument'", but im still not sure. from OCC.Core.TDocStd import TDocStd_Document from OCC.Core.XCAFApp import XCAFApp_Application h_doc = TDocStd_Document app = XCAFApp_Application.GetApplication app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),h_doc) @DarkKnight – Mohsin Bashir Aug 24 '23 at 09:40
  • if you read the error it is telling you that in this line `app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),h_doc)` the `app` is a function and not some instance with a `NewDocument` method. So if you look at where `app` is defined you should see why: you do `app = XCAFApp_Application.GetApplication` so you haven't called the method to get its result, instead you have set `app` to be a reference to the method itself – Anentropic Aug 24 '23 at 09:42
  • @Anentropic What you say is correct but doesn't account for the UnboundLocalError exception – DarkKnight Aug 24 '23 at 09:45
  • @DarkKnight yes there are two errors in the screenshot, we currently don't see the code relevant to the second error – Anentropic Aug 24 '23 at 09:46
  • @MohsinBashir here is some basic info about function and methods in Python https://www.w3schools.com/python/python_functions.asp https://www.analyticsvidhya.com/blog/2020/11/basic-concepts-object-oriented-programming-types-methods-python/#:~:text=In%20Python%2C%20methods%20are%20functions,object%2Doriented%20programming%20in%20Python. – Anentropic Aug 24 '23 at 09:47
  • Have you seen the first comment? It suggested you to try `app = XCAFApp_Application.GetApplication()`, but half hour later you still talked about `app = XCAFApp_Application.GetApplication`. The difference between `x = somefunction` and `x = somefunction()` is important. – tevemadar Aug 24 '23 at 10:14

0 Answers0