在ETAS INCA中,是否有一种方法可以从ASAP2Project中的任意路径快速检索项目? [英] In ETAS INCA, is there a way to quickly retrieve an item from an arbitrary path in an ASAP2Project?

查看:55
本文介绍了在ETAS INCA中,是否有一种方法可以从ASAP2Project中的任意路径快速检索项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过.NET API连接到INCA,以便浏览ASAP2项目的文件夹结构.具体来说,我想获得一个代表下面突出显示的目标"文件夹的对象.

I'm trying to connect to INCA through the .NET API in order to navigate the folder structure of an ASAP2 project. Specifically, I want to get an object that represents the "Target" folder that I've highlighted below.

这非常棘手-API提供了很多名称为"Folder"的类: Folder Asap2ProjectFolder IncaFolder.那么,要检索目标"文件夹,我需要知道些什么?

This is proving pretty tricky--The API provides a ton of classes that have the name "Folder" in them: Folder, Asap2ProjectFolder, and IncaFolder. So what do I need to know in order to retrieve the "Target" folder?

推荐答案

您的第一个冲动可能是认为目标文件夹存在于长路径上,例如 USER A \ Demo \ Demo03 \ Foo \ Bar \ Baz\ Target .不幸的是,INCA没有执行此操作的机制-相反,您必须检索路径 Database Objects 部分中的最低项目,然后在该项目中查询 Datasets部分.

Your first impulse might be to think of the Target folder as existing on a long path like USER A\Demo\Demo03\Foo\Bar\Baz\Target. Unfortunately, INCA doesn't have a mechanism to do this--instead, you have to retrieve the lowest item in the Database Objects portion of the path, and then query that item for the Datasets portion of the path.

从数据库根目录开始,我们需要深入到Demo03,它是一个 Asap2Project :

From the database root, we need to drill down to Demo03, which is an Asap2Project:

USER A\Demo\Demo03

然后在Demo03中,我们需要向下钻取到Target,它是一个 Asap2ProjectFolder :

From within Demo03, we then need to drill down to Target, which is an Asap2ProjectFolder:

Foo\Bar\Baz\Target

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

The first part is pretty simple--we can connect to INCA and get the Demo03 Asap2Project in just a few lines of code:

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!"
   }

现在,在这里事情变得有些棘手.

Now, here's where things get a bit tricky.

如果我们试图获取像Demo03_1这样的 DataSet ,则可以使用像 demoProject.GetDataSetForName("Demo03 \\ Demo03_1")这样的调用.问题是,此方法仅适用于DataSet类型的对象.因此,我们无法使用它来检索 Foo \ Bar \ Baz \ Target 之类的文件夹.如果您已经在API中摸索了一番,那么您可能已经尝试过以下方法:

If we were trying to get a DataSet like Demo03_1, we could use a call like demoProject.GetDataSetForName("Demo03\\Demo03_1"). The problem is, this method only works for objects of type DataSet. So we can't use it to retrieve a folder like Foo\Bar\Baz\Target. If you've poked around in the API a bit, you might have tried something like this:

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

事实证明,我们需要变得更加足智多谋.让我们为 Asap2Project Asap2ProjectFolder 类创建 GetItemInFolder()扩展方法,这样这些类将具有与相同的项检索灵活性.>数据库类:

Turns out that we need to be a bit more resourceful. Let's make GetItemInFolder() extension methods for the Asap2Project and Asap2ProjectFolder classes, so those classes will have the same item-retrieval flexibility as the DataBase class:

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;
         }
      }
   }

这使得从 Asap2Project Asap2ProjectFolder 中抓取项目变得非常容易.要获取Target文件夹,我们仅需要以下调用:

This makes it super-easy to grab items out of an Asap2Project or Asap2ProjectFolder. To grab the Target folder, we only need the following calls:

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");
   }

这篇关于在ETAS INCA中,是否有一种方法可以从ASAP2Project中的任意路径快速检索项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆