为什么 "EA.Project.GeneratePackage" 方法不生成代码?

Why "EA.Project.GeneratePackage" method does not generate the code?

我正在为 EA 开发一个插件,它可以从包中生成 C 代码。你可以在下面看到我的一段代码。我可以看到 GUID 和 XML 格式的 GUID 是正确的。但由于某种原因,GeneratePackage 方法根本不生成任何代码。你对我如何解决这个问题有什么建议吗?

public void generateCode(EA.Repository repository)
{
    // step one: get the GUID of the package you want to generate code from
    string thePackageGUID = repository.ProjectGUID;
    MessageBox.Show(thePackageGUID);

    // step two: use the package GUID to access the package - seems like it's not relevant
    //EA.Package thePackage = repository.GetPackageByGuid (thePackageGUID);
    // step three: access the project through the interface "GetProjectInterface"
    EA.Project theProject = repository.GetProjectInterface();

    // step four: convert GUID to XML format
    string GUIDinXML = theProject.GUIDtoXML(thePackageGUID);
    MessageBox.Show(GUIDinXML);
    MessageBox.Show("That was the GUID in XML format. Did u see it?");

    // step five: access the code generation utility function
    string ExtraOptions = "1";
    Boolean generationFlag = theProject.GeneratePackage(GUIDinXML, ExtraOptions);
    if (generationFlag == true)
    {
        MessageBox.Show(GUIDinXML);
    }
    else
    {
        MessageBox.Show("Sorry, C code was not generated.");
    }
}

问题是第一步

// step one: get the GUID of the package you want to generate code from
string thePackageGUID = repository.ProjectGUID;

您使用的 ProjectGUID 是您整个项目(又名模型、又名存储库)的唯一标识符,并不标识包。

您需要以某种方式拿到一个包裹。

有几种方法可以做到这一点

  • 允许您的用户 select 使用 Repository.InvokeConstructPicker
  • 的包
  • 使用 Repository.GetTreeSelectedPackage
  • 在项目浏览器中使用 selected 包
  • 使用Repository.SQLQuery找出需要生成的包
  • 使用对包 GUID 的硬编码引用
  • 使用配置文件存储程序包 GUID

这是代码的修改版本。 它允许您 select 包,并成功生成代码。

    // this function retrives the GUID of the selected item, accesses the item and initializes the EA auto code generation
    public void generateCode(EA.Repository repository)
    {
        // get the ID of the selected item (I only included Package)
        // InvokeConstructPicker can include all element types. Modify it to include more types, if needed
        int elementID = repository.InvokeConstructPicker("IncludedTypes=Package");

        // convert TypeID to string - it is needed only for displaying
        string str_elementID = Convert.ToString(elementID);
        MessageBox.Show("The element ID is:   " + str_elementID);

        // get the element knowing its ID
        EA.Element element = repository.GetElementByID(elementID);

        // now the element is known. retrive and display the element GUID
        MessageBox.Show("The element GUID is:   " + element.ElementGUID);

        // get the relevant package, knowing one of its elements and element's GUID
        // this line can be removed, if "Package" is the only type included in InvokeConstructPicker
        EA.Package package = repository.GetPackageByGuid(element.ElementGUID);

        // display the package GUID
        MessageBox.Show("The package GUID is:   " + package.PackageGUID);

        // access the project (and therefore the package) using the interface "GetProjectInterface"
        EA.Project theProject = repository.GetProjectInterface();

        // convert the package GUID to XML format
        // if "Package" is the only included type, in the line below the parameter can be switched to "element.ElementGUID"
        string GUIDinXML = theProject.GUIDtoXML(package.PackageGUID);
        MessageBox.Show("The package GUID in XML format is:   " + GUIDinXML);

        // access the code generation utility function
        bool generationFlag = theProject.GeneratePackage(GUIDinXML, "1");
        if (generationFlag == true)
        {
            MessageBox.Show("Code is generated.");
        }
        else
        {
            MessageBox.Show("Sorry, C code was not generated.");
        }
    }