Skip to content

Commit b74ed6f

Browse files
author
Brad Carman
committed
move node feature
1 parent 73a23fc commit b74ed6f

File tree

2 files changed

+118
-68
lines changed

2 files changed

+118
-68
lines changed

src/ModelingToolkitDesigner.jl

Lines changed: 117 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ TODO:
2626
# wall::Symbol
2727
# end
2828

29+
const Δh = 0.05
2930

3031
mutable struct ODESystemDesign
3132
# parameters::Vector{Parameter}
@@ -39,10 +40,20 @@ mutable struct ODESystemDesign
3940
xy::Observable{Tuple{Float64, Float64}}
4041
icon::Union{String, Nothing}
4142
color::Observable{Symbol}
42-
wall::Symbol
43+
wall::Observable{Symbol}
4344
end
4445

45-
function ODESystemDesign(rootnamespace::Union{Symbol, Nothing}, system::ODESystem, design::Dict; x=0.0, y=0.0, icon=nothing, wall=:E)
46+
function ODESystemDesign(rootnamespace::Union{Symbol, Nothing}, system::ODESystem, design::Union{Vector{Pair{ODESystem, NamedTuple}}, Dict}; x=0.0, y=0.0, icon=nothing, wall=:E)
47+
48+
if design isa Vector{Pair{ODESystem, NamedTuple}}
49+
pairs = design
50+
design = Dict()
51+
for (sys, args) in pairs
52+
push!(design, sys.name => args)
53+
end
54+
end
55+
56+
# Main._x[] = design
4657

4758
println("system: $(system.name)")
4859
if !isnothing(rootnamespace)
@@ -56,7 +67,7 @@ function ODESystemDesign(rootnamespace::Union{Symbol, Nothing}, system::ODESyste
5667

5768
children = ODESystemDesign[]
5869
connectors = ODESystemDesign[]
59-
connections = Pair[]
70+
connections = Tuple{ODESystemDesign, ODESystemDesign}[]
6071
if !isempty(systems)
6172
children_ = filter(x->!ModelingToolkit.isconnector(x), systems)
6273
if !isempty(children_)
@@ -94,14 +105,18 @@ function ODESystemDesign(rootnamespace::Union{Symbol, Nothing}, system::ODESyste
94105

95106
if eq.rhs isa Connection
96107

97-
connections = ODESystemDesign[]
108+
conns = []
98109
for connector in eq.rhs.systems
99110

100111
parent, child = split(string(connector.name), '')
101112
child_design = filtersingle(x->string(x.system.name) == parent, children)
102113
connector_design = filtersingle(x->string(x.system.name) == child, child_design.connectors)
103114

104-
push!(connections, connector_design)
115+
push!(conns, connector_design)
116+
end
117+
118+
for i=2:length(conns)
119+
push!(connections, (conns[i-1], conns[i]))
105120
end
106121

107122

@@ -118,7 +133,7 @@ function ODESystemDesign(rootnamespace::Union{Symbol, Nothing}, system::ODESyste
118133

119134

120135

121-
return ODESystemDesign(rootnamespace, system, children, connectors, connections, Equation[], xy, icon, Observable(get_color(system)), wall)
136+
return ODESystemDesign(rootnamespace, system, children, connectors, connections, Equation[], xy, icon, Observable(get_color(system)), Observable(wall))
122137
end
123138

124139
# connector => box
@@ -280,6 +295,24 @@ function view(model::ODESystemDesign)
280295
println(event.key)
281296
end
282297

298+
next_wall_button = Button(fig[11,3]; label="move node")
299+
on(next_wall_button.clicks) do clicks
300+
for system in model.systems
301+
for connector in system.connectors
302+
if connector.color[] == :pink
303+
if connector.wall[] == :N
304+
connector.wall[] = :E
305+
elseif connector.wall[] == :W
306+
connector.wall[] = :N
307+
elseif connector.wall[] == :S
308+
connector.wall[] = :W
309+
elseif connector.wall[] == :E
310+
connector.wall[] = :S
311+
end
312+
end
313+
end
314+
end
315+
end
283316

284317
fig
285318

@@ -305,9 +338,7 @@ function connect(model::ODESystemDesign)
305338

306339
push!(selected_connectors, connector)
307340

308-
on(connector.xy) do val
309-
update()
310-
end
341+
311342

312343
color = get_color(connector)
313344
connector.color[] = color
@@ -341,6 +372,11 @@ function connect(connection::Tuple{ODESystemDesign, ODESystemDesign})
341372
notify(ys)
342373
end
343374

375+
for connector in connection
376+
on(connector.xy) do val
377+
update()
378+
end
379+
end
344380

345381
update()
346382

@@ -352,7 +388,7 @@ end
352388

353389

354390

355-
get_wall(system::ODESystemDesign) = system.wall
391+
get_wall(system::ODESystemDesign) = system.wall[]
356392

357393

358394
get_text_alignment(wall::Symbol) = get_text_alignment(Val(wall))
@@ -376,7 +412,7 @@ end
376412

377413

378414
function add_system(system::ODESystemDesign)
379-
Δh = 0.05
415+
380416

381417
xso = Observable(zeros(5))
382418
yso = Observable(zeros(5))
@@ -413,82 +449,96 @@ function add_system(system::ODESystemDesign)
413449

414450
text!(ax[], xo, ylabelo; text=string(system.system.name), align=(:center, :bottom))
415451

416-
wall(y) = filter(x->get_wall(x) == y, system.connectors)
452+
draw_connector_nodes = () -> begin
453+
wall(y) = filter(x->get_wall(x) == y, system.connectors)
417454

418-
# walls = [ModelingToolkitComponents.N, ModelingToolkitComponents.S, ModelingToolkitComponents.E, ModelingToolkitComponents.W]
419-
walls = [:N, :S, :E, :W]
420-
for w in walls
455+
# walls = [ModelingToolkitComponents.N, ModelingToolkitComponents.S, ModelingToolkitComponents.E, ModelingToolkitComponents.W]
456+
for w in [:N, :S, :E, :W]
421457

422458

423-
connectors_on_wall = wall(w)
459+
connectors_on_wall = wall(w)
424460

425-
n_items = length(connectors_on_wall)
426-
delta = 2*Δh/(n_items+1)
461+
n_items = length(connectors_on_wall)
462+
delta = 2*Δh/(n_items+1)
427463

428-
#TODO: Order by item.node_order
464+
#TODO: Order by item.node_order
429465

430-
for i=1:n_items
466+
for i=1:n_items
431467

432-
if w == :N
433-
x = delta*i - Δh
434-
xt = x
435-
y = +Δh
436-
yt = y*0.6
468+
draw_connector_node(connectors_on_wall[i], delta, i, w)
469+
437470
end
438471

439-
if w == :S
440-
x = delta*i - Δh
441-
xt = x
442-
y = -Δh
443-
yt = y*0.6
444-
end
445472

446-
if w == :E
447-
x = +Δh
448-
xt = x*1.4
449-
y = delta*i - Δh
450-
yt = y
451-
end
473+
end
474+
end
452475

453-
if w == :W
454-
x = -Δh
455-
xt = x*1.4
456-
y = delta*i - Δh
457-
yt = y
458-
end
476+
draw_connector_nodes()
459477

460-
xpo = Observable(0.0)
461-
on(xo) do val
462-
xpo[] = val + x
463-
connectors_on_wall[i].xy[] = (xpo[], connectors_on_wall[i].xy[][2])
464-
end
478+
for connector in system.connectors
479+
on(connector.wall) do val
480+
draw_connector_nodes()
481+
end
482+
end
465483

466-
ypo = Observable(0.0)
467-
on(yo) do val
468-
ypo[] = val + y
469-
connectors_on_wall[i].xy[] = (connectors_on_wall[i].xy[][1], ypo[])
470-
end
484+
end
471485

472-
xto = Observable(0.0)
473-
on(xo) do val
474-
xto[] = val + xt
475-
end
486+
function draw_connector_node(connector::ODESystemDesign, delta, i, w)
487+
if w == :N
488+
x = delta*i - Δh
489+
xt = x
490+
y = +Δh
491+
yt = y*0.6
492+
end
476493

477-
yto = Observable(0.0)
478-
on(yo) do val
479-
yto[] = val + yt
480-
end
494+
if w == :S
495+
x = delta*i - Δh
496+
xt = x
497+
y = -Δh
498+
yt = y*0.6
499+
end
481500

482-
501+
if w == :E
502+
x = +Δh
503+
xt = x*1.4
504+
y = delta*i - Δh
505+
yt = y
506+
end
483507

484-
scatter!(ax[], xpo, ypo; marker=:rect, color = connectors_on_wall[i].color, markersize=15)
485-
text!(ax[], xto, yto; text=string(connectors_on_wall[i].system.name), color = get_color(connectors_on_wall[i]), align=get_text_alignment(w), fontsize=10)
486-
487-
end
508+
if w == :W
509+
x = -Δh
510+
xt = x*1.4
511+
y = delta*i - Δh
512+
yt = y
513+
end
488514

515+
xpo = Observable(0.0)
516+
on(xo) do val
517+
xpo[] = val + x
518+
connector.xy[] = (xpo[], connector.xy[][2])
519+
end
489520

490-
end
521+
ypo = Observable(0.0)
522+
on(yo) do val
523+
ypo[] = val + y
524+
connector.xy[] = (connector.xy[][1], ypo[])
525+
end
526+
527+
xto = Observable(0.0)
528+
on(xo) do val
529+
xto[] = val + xt
530+
end
491531

532+
yto = Observable(0.0)
533+
on(yo) do val
534+
yto[] = val + yt
535+
end
536+
537+
538+
539+
scatter!(ax[], xpo, ypo; marker=:rect, color = connector.color, markersize=15)
540+
text!(ax[], xto, yto; text=string(connector.system.name), color = get_color(connector), align=get_text_alignment(w), fontsize=10)
541+
492542
end
493543

494544

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function system(N; name, fluid)
5252
eqs = [
5353
connect(stp.output, src.input)
5454
connect(src.port, res.port_a)
55-
connect(res.port_b, vol.port)
55+
# connect(res.port_b, vol.port)
5656
]
5757

5858
ODESystem(eqs, t, [], pars; name, systems)

0 commit comments

Comments
 (0)