Working with WBS Associated with Tasks
WBS (Work Breakdown Structure) codes, lets you assign outline numbers to tasks according to the needs of your business. It’s a method for applying a customized outline scheme to a project. Aspose.Tasks for C++ API supports this feature.
Working with Work Breakdown Structure
The WBS and WBSLevel properties exposed by the Tsk class are used to read and write a tasks work breakdown structure.
Microsoft Project View of WBS
To view WBS information in Microsoft Project:
- On the Task Entry Form, select the Insert menu and then Column.
- Add the WBS column.
Getting WBS in Aspose.Tasks
The following examples show how to get a task’s WBS value using Aspose.Tasks.
1// Read project
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3System::SharedPtr<Project> project1 = System::MakeObject<Project>(dataDir + u"TaskWBS.mpp");
4
5// Create a ChildTasksCollector instance
6System::SharedPtr<ChildTasksCollector> collector = System::MakeObject<ChildTasksCollector>();
7
8// Collect all the tasks from RootTask using TaskUtils
9TaskUtils::Apply(project1->get_RootTask(), collector, 0);
10
11// Parse through all the collected tasks
12
13{
14 auto tsk_enumerator = (collector->get_Tasks())->GetEnumerator();
15 decltype(tsk_enumerator->get_Current()) tsk;
16 while (tsk_enumerator->MoveNext() && (tsk = tsk_enumerator->get_Current(), true))
17 {
18 System::Console::WriteLine(tsk->Get(Tsk::WBS()));
19 System::Console::WriteLine(tsk->Get(Tsk::WBSLevel()));
20
21 // Set custom WBS
22 tsk->Set<System::String>(Tsk::WBS(), System::String(u"custom wbs") + tsk->Get(Tsk::WBS()));
23 }
24}
Adding WBS Code Definitions
Aspose.Tasks for C++ API allows adding WBS code definition for tasks to a project same as Microsoft Project.WBSCodeDefinition class exposes various properties that can be used to generate desired WBS codes in a project. The following code sample demonstrates setting up WBS code definitions in a project.
1System::SharedPtr<Project> proj = System::MakeObject<Project>();
2
3proj->set_WBSCodeDefinition(System::MakeObject<WBSCodeDefinition>());
4proj->get_WBSCodeDefinition()->set_GenerateWBSCode(true);
5proj->get_WBSCodeDefinition()->set_VerifyUniqueness(true);
6proj->get_WBSCodeDefinition()->set_CodePrefix(u"CRS-");
7
8System::SharedPtr<WBSCodeMask> mask = System::MakeObject<WBSCodeMask>();
9mask->set_Length(2);
10mask->set_Separator(u"-");
11mask->set_Sequence(Aspose::Tasks::WBSSequence::OrderedNumbers);
12proj->get_WBSCodeDefinition()->get_CodeMaskCollection()->Add(mask);
13
14mask = System::MakeObject<WBSCodeMask>();
15mask->set_Length(1);
16mask->set_Separator(u"-");
17mask->set_Sequence(Aspose::Tasks::WBSSequence::OrderedUppercaseLetters);
18proj->get_WBSCodeDefinition()->get_CodeMaskCollection()->Add(mask);
19
20System::SharedPtr<Task> tsk = proj->get_RootTask()->get_Children()->Add(u"Task 1");
21System::SharedPtr<Task> child = tsk->get_Children()->Add(u"Task 2");
22
23proj->Recalculate();
24proj->Save(dataDir + u"AddWBSCodes_out.xml", Aspose::Tasks::Saving::SaveFileFormat::XML);
Renumber WBS codes
Aspose.Tasks for C++ API is able to renumber WBS codes similar to MSP’s “Renumber” function.
1auto project = System::MakeObject<Project>(dataDir + u"RenumberExample.mpp");
2
3auto tasks = project->get_RootTask()->SelectAllChildTasks();
4
5System::Console::WriteLine(u"WBS codes before: ");
6
7// output: ""; "1"; "2"; "4"
8
9{
10 auto task_enumerator = (tasks)->GetEnumerator();
11 decltype(task_enumerator->get_Current()) task;
12 while (task_enumerator->MoveNext() && (task = task_enumerator->get_Current(), true))
13 {
14 System::Console::WriteLine(System::String(u"\"") + task->Get(Tsk::WBS()) + u"\"" + u"; ");
15 }
16}
17
18// project.RenumberWBSCode(); // this overload could have used too
19project->RenumberWBSCode([&]{ int32_t init_0[] = {1, 2, 3}; auto list_0 = System::MakeObject<System::Collections::Generic::List<int32_t>>(); list_0->AddInitializer(3, init_0); return list_0; }());
20
21System::Console::WriteLine(u"\nWBS codes after: ");
22
23// output: ""; "1"; "2"; "3"
24
25{
26 auto task_enumerator = (tasks)->GetEnumerator();
27 decltype(task_enumerator->get_Current()) task;
28 while (task_enumerator->MoveNext() && (task = task_enumerator->get_Current(), true))
29 {
30 System::Console::WriteLine(System::String(u"\"") + task->Get(Tsk::WBS()) + u"\"" + u"; ");
31 }
32}