exponenta event banner

Создание проекта и ссылка на него программным способом

В этом примере показано, как программно создать новый проект и добавить его в качестве ссылочного проекта в основной проект. В нем описывается, как создать проект из командной строки, добавить файлы и папки, настроить путь к проекту, определить ярлыки проекта и создать ссылку на новый проект в другом проекте.

Настройка файлов примеров

1. Создание рабочей копии Airframe Example и откройте проект. MATLAB ® копирует файлы в папку примеров, чтобы их можно было редактировать. ИспользоватьcurrentProject для создания объекта проекта из загруженного проекта.

sldemo_slproject_airframe_api;
Starting: Simulink
mainProject = currentProject
mainProject = 
  Project with properties:

                        Name: "Airframe Example"
    SourceControlIntegration: "Git"
          RepositoryLocation: "C:\workSpace\examples\repositories\airRef8"
       SourceControlMessages: ["No remote tracking branch"    "Current branch: master"    "Branch status: Normal"]
                    ReadOnly: 0
                    TopLevel: 1
                Dependencies: [1×1 digraph]
                  Categories: [1×1 matlab.project.Category]
                       Files: [1×22 matlab.project.ProjectFile]
                   Shortcuts: [1×5 matlab.project.Shortcut]
                 ProjectPath: [1×4 matlab.project.PathFolder]
           ProjectReferences: [1×1 matlab.project.ProjectReference]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
                 Description: "This example project demonstrates the Project referencing feature."
                  RootFolder: "C:\workSpace\examples\airRef8\main"
       SimulinkCodeGenFolder: "C:\workSpace\examples\airRef8\main\work\codegen"
         SimulinkCacheFolder: "C:\workSpace\examples\airRef8\main\work\cache"
        ProjectStartupFolder: "C:\workSpace\examples\airRef8\main"

Airframe Example проект является проектом верхнего уровня (TopLevel: 1) с одним проектом, на который имеется ссылка (ProjectReferences: [1x1]).

Создать новый проект

2. Создать новый проект с именем Wind Gust Library. Airframe проект будет использовать Wind Gust Library через ссылку на проект.

a. Создайте пустой проект и задайте имя проекта.

windGustFolder = fullfile(mainProject.RootFolder, "..", "refs", "Wind Gust Library");
windGust = matlab.project.createProject(windGustFolder);
windGust.Name = "Wind Gust Library";

b. Добавить data папку и wind_gust_lib.slx в файл Wind Gust Library проект.

addFolderIncludingChildFiles(windGust, "data");
addFile(windGust, "wind_gust_lib.slx");

c. Добавить data папку и Wind Gust Library корневой папки проекта в Wind Gust Library путь к проекту. Это делает файлы доступными, когда Airframe Example проект или любой проект, который ссылается на Wind Gust Library проект загружен.

addPath(windGust, "data");
addPath(windGust, windGust.RootFolder);

d. Создать Wind Gust Library ярлык проекта.

shortcut = addShortcut(windGust, "wind_gust_lib.slx");
shortcut.Group = "Top Level Model";

Добавление ссылки на проект

3. Добавить новый Wind Gust Library в проект Airframe Example проект как ссылка на проект. Это позволяет Airframe Example для просмотра, редактирования и запуска файлов в Wind Gust Library проект.

reload(mainProject);
addReference(mainProject, windGust)
ans = 
  ProjectReference with properties:

           Project: [1×1 matlab.project.Project]
              File: "C:\workSpace\examples\airRef8\refs\Wind Gust Library"
    StoredLocation: "../refs/Wind Gust Library"
              Type: "Relative"

Основной проект Airframe Example ссылается на Wind Gust Library хранится в "../refs/Wind Gust Library".

4. Использовать ProjectReferences метод для запроса Wind Gust Library проект.

mainProject.ProjectReferences(2).Project
ans = 
  Project with properties:

                        Name: "Wind Gust Library"
    SourceControlIntegration: ""
          RepositoryLocation: ""
       SourceControlMessages: [1×0 string]
                    ReadOnly: 1
                    TopLevel: 0
                Dependencies: [1×1 digraph]
                  Categories: [1×1 matlab.project.Category]
                       Files: [1×3 matlab.project.ProjectFile]
                   Shortcuts: [1×1 matlab.project.Shortcut]
                 ProjectPath: [1×2 matlab.project.PathFolder]
           ProjectReferences: [1×0 matlab.project.ProjectReference]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
                 Description: ""
                  RootFolder: "C:\workSpace\examples\airRef8\refs\Wind Gust Library"

Wind Gust Library проект не является проектом верхнего уровня (TopLevel: 0). На него ссылается проект верхнего уровня Airframe Example (TopLevel: 1).

См. также

Компоновка крупных проектов