Import from dhtmlxGantt#
Load a dhtmlxGantt project into Planning Engine and get the same task dates the gantt auto-scheduler would give you.
Planning Engine's scheduling step is a port of the dhtmlxGantt v2 auto-scheduling engine. Its records use gantt's fields under PascalCase names (StartDate, Duration, Parent, ConstraintType, AutoScheduling), so most of the import copies fields one to one. The steps below cover the places where a straight copy gives different dates or fails validation.
Prerequisites#
- Your gantt tasks, links, resources and assignments are parsed into your own C# records.
Plen.Corehas no JSON input. To read gantt-style JSON directly, use the separatePlen.Wirepackage. See Exchange JSON with a client. - You know each task's effective calendar, as gantt resolves it.
Steps#
-
Set the scheduling config to gantt's model. Use
CalendarMode.Taskfor task calendars only, orCalendarMode.TaskInheritedif gantt hasinherit_calendar: true. Copy gantt'swork_timeintoUseCalendarsexplicitly. If you leaveUseCalendarsunset, it staystrueand Planning Engine schedules on calendars:var ganttWorkTime = true; // gantt `config.work_time` var data = new ProjectData { Config = { CalendarMode = CalendarMode.Task, UseCalendars = ganttWorkTime, }, };Set
RootIdonly when gantt's virtual root isn't0. The other auto-scheduling options (ApplyConstraints,GapBehavior,MoveProjects,ScheduleFromEnd,ProjectStart,UseProgress) are gantt's options under PascalCase names. -
Add one
CalendarDataper calendar your tasks use, and set each task'sCalendarIdto its effective calendar. gantt picks that calendar in its adapter: the task's own, else its resource's, else a calendar merged from all its resources, else the parent's. Send the result, not the rules. A merged resource calendar goes in once, under its synthetic id. Don't repeat gantt's precedence or merging in your import code. -
Map each task to a
TaskData:- gantt
type: "project"becomesTaskType.Summary. A summary takes bothStartDateandEndDate, or neither. Drop itsDuration, or strict validation rejects the task. - A
Parentequal toRootIdmeans "no parent". You can copy it as is. - A leaf task needs two of
StartDate,EndDateandDuration. If you set all three, they must agree. - If your gantt event handlers cancel auto-scheduling for a task, set
AutoScheduling = falseon it. The planner never moves it.
- gantt
-
Map gantt link
typevalues"0","1","2"and"3"toLinkType.Fs,Ss,FfandSf. Copylagand leaveLagUnitasnull, because gantt measures lag inconfig.duration_unit. -
Convert each assignment to a slice of its task's work in
AssignmentData. gantt'smodehas no counterpart, so don't carry it over.defaultandfixedDuration:delaybecomesOffsetanddurationstaysDuration. Leave bothnullto book the whole task.fixedDates: setOffsetandDurationto the worked time its dates cover, measured from the task start.
A slice ends at the task end. gantt's
fixedDurationcan run past it. -
Solve the data with
Project.Solveand write the changed fields fromresult.Updatesback into gantt. The updates include summaries, which the gantt JS adapter leaves to gantt core. Apply those too, or your summary spans go stale.
This example imports a summary with two tasks, a milestone, two links, a part-time calendar and one default-mode assignment:
using System.Globalization;
using Plen;
GanttTask[] ganttTasks =
[
new(1, 0, "project", "2026-03-02 00:00", "2026-03-07 00:00", 5, "global"),
new(2, 1, "task", "2026-03-02 00:00", null, 3, "global"),
new(3, 1, "task", "2026-03-02 00:00", null, 4, "part-time"),
new(4, 0, "milestone", "2026-03-02 00:00", null, 0, "global"),
];
GanttLink[] ganttLinks =
[
new(1, 2, 3, "0", 0),
new(2, 1, 4, "0", 0),
];
GanttAssignment[] ganttAssignments =
[
new(1, 3, "dev", 1.0, Delay: 1, Duration: 2),
];
var data = new ProjectData
{
Config =
{
CalendarMode = CalendarMode.Task, // gantt's calendar model
UseCalendars = true, // gantt `config.work_time`
},
Calendars =
[
new CalendarData { Id = "global" },
new CalendarData
{
Id = "part-time",
Worktime = new Worktime { Days = [false, true, true, true, true, false, false] }, // Mon-Thu
},
],
Tasks = ganttTasks.Select(ToTask).ToList(),
Links = ganttLinks.Select(g => new LinkData
{
Id = g.Id,
Source = g.Source,
Target = g.Target,
LinkType = ToLinkType(g.Type),
Lag = g.Lag, // LagUnit stays null: gantt lag is in config.duration_unit
}).ToList(),
Resources = [new ResourceData { Id = "dev" }],
Assignments = ganttAssignments.Select(g => new AssignmentData
{
Id = g.Id,
Task = g.TaskId,
Resource = g.ResourceId,
Value = g.Value,
Offset = g.Delay,
Duration = g.Duration,
}).ToList(),
};
var project = new Project();
var result = project.Solve(data, new SolveOptions());
foreach (var m in result.Updates)
{
// Only changed fields are set. Summaries are included: write them back too.
if (m.StartDate is { } start) Console.WriteLine($"{m.Id} start_date = {start}");
if (m.EndDate is { } end) Console.WriteLine($"{m.Id} end_date = {end}");
}
static Moment ParseDate(string s) =>
// Use the format your gantt `date_format` produces.
Moment.FromDateTime(DateTime.ParseExact(s, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture));
static LinkType ToLinkType(string type) => type switch
{
"0" => LinkType.Fs,
"1" => LinkType.Ss,
"2" => LinkType.Ff,
"3" => LinkType.Sf,
_ => throw new ArgumentException($"unknown gantt link type {type}"),
};
static TaskData ToTask(GanttTask g)
{
var t = new TaskData(g.Id)
{
Parent = g.Parent, // gantt's root (0) means "no parent"
StartDate = ParseDate(g.StartDate),
CalendarId = g.CalendarId,
};
switch (g.Type)
{
case "project":
// A summary takes both dates or neither, never a duration.
t.TaskType = TaskType.Summary;
if (g.EndDate is null) t.StartDate = null;
else t.EndDate = ParseDate(g.EndDate);
break;
case "milestone":
t.TaskType = TaskType.Milestone;
t.Duration = 0;
break;
default:
t.Duration = g.Duration;
break;
}
return t;
}
// Records as your server holds them after parsing gantt's JSON.
record GanttTask(int Id, int Parent, string Type, string StartDate, string? EndDate, double Duration, string CalendarId);
record GanttLink(int Id, int Source, int Target, string Type, double Lag);
record GanttAssignment(int Id, int TaskId, string ResourceId, double Value, double? Delay, double? Duration);
Keep gantt's triggers on your side#
Planning Engine has no UI triggers and no events. Your code decides when to schedule.
- Call
Project.Applyand thenSolve(null, …)wherever gantt would run auto-scheduling: after the user adds a link, drags a task, or saves the lightbox. See Editing a project. gantt.autoSchedule(id, inclusive)isSolveOptions.Subtree. See Reschedule a branch.- Change sets can't add or edit calendars. If an assignment change gives a task a new merged calendar, send the whole project again with
Solve(data, …). - gantt core rewrites descendant dates when inherited calendars or effort change. Forward those date and duration edits as ordinary
TaskUpdatechanges. Planning Engine doesn't recreate gantt's effort rules.
Differences to expect#
- Strict validation rejects input that gantt coerces: duplicate ids, a dated constraint with no
ConstraintDate, a leaf task with fewer than two of start, end and duration, a dateless summary with no dated children. To get gantt's coercions with a report of each fix, see Accept messy input. TimeUnithas noweek,monthoryear, and calendars withcustomWeekscan't be represented. A gantt merged calendar can containcustomWeeks, so such projects can't be imported yet.Momentis a naive local date-time with no timezone and no DST. gantt's JS engine uses the host time zone, so its arithmetic follows DST.Moment.FromDateTimereads the date and time fields as they are and ignoresDateTime.Kind, so convert UTC values to local time before you build the data.result.Noteslists the links, tasks and assignments the engine dropped. gantt drops them silently.- Three cases give different dates on purpose. With
ProjectConstraint = true, children inherit the parent's constraint. gantt's engine ignores it. WithUseProgress, a completed child of a moving summary stays in place. gantt drags it along. WithDescendantLinks = truein block mode, gantt fails with a cycle error and Planning Engine schedules the project. See Links, constraints and summaries. - Summary-to-summary links in block mode, combined with leaf links between the same summaries, can give different dates. On very large generated graphs the dates drift out of range and the call throws
InvalidOperationException. See Error handling.
Result#
The example prints the changed fields:
3 start_date = 2026-03-05 00:00
3 end_date = 2026-03-12 00:00
1 end_date = 2026-03-12 00:00
4 start_date = 2026-03-12 00:00
4 end_date = 2026-03-12 00:00
Task 3 starts when task 2 finishes and works Monday to Thursday only, so it ends a week later. The summary 1 grows to cover it. Task 2 didn't move, so it isn't listed. To get every date on the first load, see Load all task dates.