Book a Demo

Author Topic: C++ example of Add-In with Model Search?  (Read 6212 times)

Fred Walter

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
C++ example of Add-In with Model Search?
« on: January 25, 2011, 02:53:38 am »
I need to add a method to the Add-In that I'm working on, that is called from Model Search.

Here is the documentation that is provided by Sparx:

http://www.sparxsystems.com/enterprise_architect_user_guide/automation_and_scripts/add_in_search.html

If my method name was "FindSuspectTraces" then what should the C++ function prototype be? How should I set XMLResults?

alesliehughes

  • EA Administrator
  • EA User
  • *****
  • Posts: 104
  • Karma: +0/-0
    • View Profile
Re: C++ example of Add-In with Model Search?
« Reply #1 on: January 25, 2011, 08:39:43 am »
The XMLResults should be a string.  The format of the string can be found at http://www.sparxsystems.com/enterprise_architect_user_guide/automation_and_scripts/xml_format_search_data.html

Alistair

Fred Walter

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: C++ example of Add-In with Model Search?
« Reply #2 on: January 25, 2011, 08:56:12 am »
Here is how I've interpreted the docs:

--- .idl file entry
HRESULT FindSuspectTraces(IDispatch *Repository, BSTR SearchText, [out] BSTR *XMLResults);

--- function definition
HRESULT STDMETHODCALLTYPE CAddIn::FindSuspectTraces(IDispatch *Repository, BSTR SearchText, BSTR *XMLResults)
{
        wstring wXML;
        wXML  = L"<ReportViewData>\n";
        wXML += L" <Fields>\n";
        wXML += L"  <Field name=\"Object\"/>\n";
        wXML += L"  <Field name=\"CLASSGUID\"/>\n";
        wXML += L" </Fields>\n";
        wXML += L" <Rows>\n";

        vector<wstring> modelGuids;
        modelRepo->GetModelGuids(&modelGuids);
        for(vector<wstring>::iterator mgi=modelGuids.begin(); mgi!=modelGuids.end(); mgi++) {
                wstring suspects;
                modelRepo->GetIntegritySetting(mgi->c_str(), mgi->c_str(), L"suspects", suspects);
                if (!suspects.empty()) {
                        wchar_t *suspects_buf = _wcsdup(suspects.c_str());
                        for(wchar_t *p = suspects_buf; *p;) {
                                wchar_t *pend = wcschr(p, L',');
                                if (pend == NULL)
                                        break;
                                *pend++ = L'\0';
                                wchar_t *pend2 = wcschr(pend, L'\n');
                                if (pend2 == NULL)
                                        break;
                                *pend2++ = L'\0';

                                wXML += L"  <Row>\n";
                                wXML += L"   <Field name=\"CLASSGUID\" value=\"";
                                wXML += p;
                                wXML += L"\"/>\n";
                                wXML += L"   <Field name=\"Object\" value=\"";
                                wXML += pend;
                                wXML += L"\"/>\n";
                                wXML += L"  </Row>\n";

                                p = pend2;
                        }
                        free(suspects_buf);
                }
        }

        wXML += L" </Rows>\n";
        wXML += L"</ReportViewData>\n";

        BSTR bXML = ::SysAllocString(wXML.c_str());
        *XMLResults = bXML;

        return S_OK;
}

--- XML that the above function produces
<ReportViewData>
 <Fields>
  <Field name="Object"/>
  <Field name="CLASSGUID"/>
 </Fields>
 <Rows>
  <Row>
   <Field name="CLASSGUID" value="{991DC3C2-DC4C-4faa-B848-4E9E91667287}"/>
   <Field name="Object" value="Use Case Model"/>
  </Row>
 </Rows>
</ReportViewData>

Yet nothing is displayed in the Model Search window when I execute the "Find Suspect Traces" search. I know that my function is being called, so I suspect that I've interpreted the docs incorrectly, and don't have the function declaration correct and/or I'm not setting XMLResults correctly.
« Last Edit: January 25, 2011, 10:51:43 am by FredWalter »

alesliehughes

  • EA Administrator
  • EA User
  • *****
  • Posts: 104
  • Karma: +0/-0
    • View Profile
Re: C++ example of Add-In with Model Search?
« Reply #3 on: January 25, 2011, 10:32:28 am »
Please find the corrections you will need to get search working for C++.  The return value (vRet) must be non VT_EMPTY, I've just made it an empty string.

---.idl
HRESULT Search_FindSuspectTraces([in] IRepository* pRepository, [in] BSTR Search, [out] BSTR *XMLResults, [out,retval] VARIANT *vRet);

---.cpp
FindSuspectTraces(IRepository *Repository, BSTR SearchText, BSTR *XMLResults, VARIANT *bOut)
{
    ....
    *XMLResults = ::SysAllocString(wXML.c_str());
    BSTR bXML = ::SysAllocStringLen(NULL, 0);
    V_VT(bOut) = VT_BSTR;
    V_BSTR(bOut) = bXML;

}

Alistair

Fred Walter

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: C++ example of Add-In with Model Search?
« Reply #4 on: January 25, 2011, 10:51:01 am »
Thanks, that did it.

Now, on a slightly related topic, is there an easy way, given a CLASSGUID, of getting all the other fields that should be present in the output of a search?

alesliehughes

  • EA Administrator
  • EA User
  • *****
  • Posts: 104
  • Karma: +0/-0
    • View Profile
Re: C++ example of Add-In with Model Search?
« Reply #5 on: January 25, 2011, 11:21:06 am »
Thanks, that did it.
Quote
Now, on a slightly related topic, is there an easy way, given a CLASSGUID, of getting all the other fields that should be present in the output of a search?
What fields should be displayed is up to you. The XML you create will determined what data will be displayed.  

The Repository Interface has the function to retreive object information (GetElementByGuid).

Alistair

alesliehughes

  • EA Administrator
  • EA User
  • *****
  • Posts: 104
  • Karma: +0/-0
    • View Profile
Re: C++ example of Add-In with Model Search?
« Reply #6 on: January 25, 2011, 02:15:20 pm »
1. To add the stereotype column, just add another field. Then add a Row/Field for this data.

2. A field of type CLASSTYPE will display an icon in the search list, And yes removing the ot would work.

Alistair

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C++ example of Add-In with Model Search?
« Reply #7 on: January 25, 2011, 11:58:24 pm »
Have you tried the manual?Element

Geert

Fred Walter

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: C++ example of Add-In with Model Search?
« Reply #8 on: January 26, 2011, 12:09:02 am »
Quote
Have you tried the manual?Element
Geert

Yes, I have tried the manual. It is less than obvious in some places, especially for people using C++, which is why I am asking questions here.

Alistair's responses have been helpful.