在ETAS INCA中,有没有办法从ASAP2Project中的任意路径快速检索项目?

时间:2013-08-13 16:57:17

标签: .net extension-methods calibration

我正在尝试通过.NET API连接到INCA,以便导航ASAP2项目的文件夹结构。具体来说,我想得到一个代表我在下面突出显示的“目标”文件夹的对象。

enter image description here

这非常棘手 - API提供了大量名为“Folder”的类:FolderAsap2ProjectFolderIncaFolder。那么我需要知道什么才能检索“目标”文件夹?

1 个答案:

答案 0 :(得分:0)

您的第一个冲动可能是将目标文件夹视为存在于USER A\Demo\Demo03\Foo\Bar\Baz\Target之类的长路径上。遗憾的是,INCA没有这样做的机制 - 相反,您必须检索路径的数据库对象部分中的最低项目,然后查询该项目的数据集路径的一部分。

从数据库根目录,我们需要向下钻取到Demo03,这是一个Asap2Project

USER A\Demo\Demo03

从Demo03中,我们需要深入到目标,这是一个Asap2ProjectFolder

Foo\Bar\Baz\Target

第一部分非常简单 - 我们可以连接到INCA,只需几行代码即可获得Demo03 Asap2Project:

Asap2ProjectFolder targetFolder = null;
Inca myIncaInstance = new Inca();
DataBase myDB = myIncaInstance.GetCurrentDataBase();
DataBaseItem tempItem = myDB.GetItemInFolder("Demo03", "USER A\\Demo");

if ((tempItem != null) && 
    (tempItem.IsAsap2Project()))
   {
   Asap2Project demoProject = (Asap2Project)tempItem;

   // Next step: grab the "Target folder!"
   }

现在,这里的事情变得有点棘手。

如果我们尝试获得像{0}一样的DataSet,我们可以使用demoProject.GetDataSetForName("Demo03\\Demo03_1")这样的调用。问题是,此方法仅适用于DataSet类型的对象。所以我们不能用它来检索像Foo\Bar\Baz\Target这样的文件夹。如果您在API中稍微探讨了一下,您可能尝试过这样的事情:

Asap2ProjectFolder tempProjFolder = demoProject.GetTopFolderNamed("Foo");
tempItem = tempProjFolder.GetDataBaseItem("Bar\\Baz\\Target");  // Won't work.
tempItem = tempProjFolder.GetSubFolder("Bar\\Baz\\Target");     // This won't either.

事实证明,我们需要更多的资源。我们为GetItemInFolder()Asap2Project类制作Asap2ProjectFolder扩展方法,因此这些类与DataBase类具有相同的项检索灵活性:

namespace IncaExtensions
   {
   public static class GetItemInFolderExtensions
      {
      /// <summary>
      /// Gets a DataBaseItem from an Asap2Project in an arbitrary subfolder.
      /// </summary>
      /// <param name="project">The current Asap2Project</param>
      /// <param name="itemName">The name of the item that we want to retrieve</param>
      /// <param name="folderName">The path, delimited by backslashes ('\\')</param>
      /// <returns>A DataBaseItem matching the request.  
      /// If no matching item is found, or if the path is invalid, return null.
      /// </returns>
      public static DataBaseItem GetItemInFolder(this Asap2Project project, 
                                                 string itemName, 
                                                 string folderName)
         {
         DataBaseItem returnItem = null;

         if ((folderName != null) &&
             (itemName != null))
            {
            string folderToken = PluckPathToken(ref folderName);
            Asap2ProjectFolder subFolder = 
               project.GetTopFolderNamed(folderToken);

            if (subFolder != null)
               {
               returnItem = subFolder.GetItemInFolder(itemName, folderName); 
               }
            }

         return returnItem;
         }


      /// <summary>
      /// Recursive call that returns a DataBaseItem in the target path.
      /// </summary>
      /// <param name="folder">The Asap2ProjectFolder to drill down</param>
      /// <param name="itemName">The name of the item that we want to retrieve</param>
      /// <param name="folderName">The path, delimited by backslashes ('\\')</param>
      /// <returns>A DataBaseItem matching the request.  
      /// If no matching item is found, or if the path is invalid, return null.
      /// </returns>
      public static DataBaseItem GetItemInFolder(this Asap2ProjectFolder folder, 
                                                 string itemName, 
                                                 string folderName)
         {
         DataBaseItem returnItem = null;

         if ((folderName != null) &&
             (itemName != null))
            {
            string folderToken = PluckPathToken(ref folderName);
            Asap2ProjectFolder subFolder = 
               (Asap2ProjectFolder)folder.GetSubFolder(folderToken);

            if (subFolder != null)
               {
               returnItem = subFolder.GetItemInFolder(itemName, folderName);
               }
            }
         else
            {
            returnItem = folder.GetDataBaseItem(itemName);
            }

         return returnItem;
         }


      /// <summary>
      /// Removes a backslash-delimited token from a string and shortens the
      /// input string.
      /// </summary>
      /// <param name="folderName">Backslash-delimited path.  This will be 
      /// shortened each time a token is removed.</param>
      /// <returns>The latest path token</returns>
      static string PluckPathToken(ref string folderName)
         {
         int slashIdx = folderName.IndexOf('\\');

         // If folderName has path tokens, extract the first token and
         // shorten folderName accordingly.
         string folderToken = (slashIdx > -1) ? 
            folderName.Substring(0, slashIdx) : folderName;
         folderName = (slashIdx > -1) ? 
            folderName.Substring(slashIdx + 1) : null;

         return folderToken;
         }
      }
   }

这样可以非常轻松地从Asap2ProjectAsap2ProjectFolder中抓取项目。要获取Target文件夹,我们只需要以下调用:

using IncaExtensions;
...
Asap2ProjectFolder targetFolder = null;
Inca myIncaInstance = new Inca();
DataBase myDB = myIncaInstance.GetCurrentDataBase();
DataBaseItem tempItem = myDB.GetItemInFolder("Demo03", "USER A\\Demo");

if ((tempItem != null) && 
    (tempItem.IsAsap2Project()))
   {
   Asap2Project demoProject = (Asap2Project)tempItem;
   targetFolder = (Asap2ProjectFolder)demoProject.GetItemInFolder("Target", "Foo\\Bar\\Baz");
   }