forked from HEL/circuiteria
		
	added checks + fixed auto ids
This commit is contained in:
		| @@ -11,26 +11,40 @@ | |||||||
| #let circuit(body, length: 2em) = { | #let circuit(body, length: 2em) = { | ||||||
|   let next-id = 0 |   let next-id = 0 | ||||||
|   let elements = (:) |   let elements = (:) | ||||||
|  |   let ids = () | ||||||
|  |   for e in body { | ||||||
|  |     if type(e) == dictionary and "id" in e { | ||||||
|  |       ids.push(e.id) | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|   for element in body { |   for element in body { | ||||||
|     let internal = type(element) == dictionary and "id" in element |     let internal = type(element) == dictionary and "id" in element | ||||||
|     let eid = if internal {element.id} else {none} |     let eid = if internal {element.id} else {auto} | ||||||
|     if eid == none { |     if eid == auto { | ||||||
|       while str(next-id) in elements { |       while str(next-id) in ids { | ||||||
|         next-id += 1 |         next-id += 1 | ||||||
|       } |       } | ||||||
|       eid = str(next-id) |       eid = str(next-id) | ||||||
|  |       ids.push(eid) | ||||||
|       if internal { |       if internal { | ||||||
|         element.id = eid |         element.id = eid | ||||||
|       } |       } | ||||||
|       next-id += 1 |       next-id += 1 | ||||||
|     } |     } | ||||||
|  |     if eid in elements { | ||||||
|  |       panic("An element with the id '" + eid + "' already exists. Please use a different id") | ||||||
|  |     } | ||||||
|     elements.insert(eid, element) |     elements.insert(eid, element) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   for element in elements.values() { |   for element in elements.values() { | ||||||
|     if type(element) == dictionary and "pre-process" in element { |     if type(element) == dictionary and "pre-process" in element { | ||||||
|       elements = (element.pre-process)(elements, element) |       elements = (element.pre-process)(elements, element) | ||||||
|  |       assert( | ||||||
|  |         type(elements) == dictionary, | ||||||
|  |         message: "The `pre-process` method of element '" + element.id + "' did not return the elements dictionary" | ||||||
|  |       ) | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -147,7 +147,19 @@ | |||||||
|  |  | ||||||
|   // Workaround because CeTZ needs to have all draw functions in the body |   // Workaround because CeTZ needs to have all draw functions in the body | ||||||
|   let func = {} |   let func = {} | ||||||
|   (func, bounds) = draw-shape(elmt, bounds) |   let res = draw-shape(elmt, bounds) | ||||||
|  |   assert( | ||||||
|  |     type(res) == array and res.len() == 2, | ||||||
|  |     message: "The drawing function of element '" + elmt.id + "' did not return a function and new bounds" | ||||||
|  |   ) | ||||||
|  |   (func, bounds) = res | ||||||
|  |   if type(func) == function { | ||||||
|  |     func = (func,) | ||||||
|  |   } | ||||||
|  |   assert( | ||||||
|  |     type(bounds) == dictionary, | ||||||
|  |     message: "The drawing function of element '" + elmt.id + "' did not return the correct bounds dictionary" | ||||||
|  |   ) | ||||||
|   func |   func | ||||||
|  |  | ||||||
|   if elmt.name != none { |   if elmt.name != none { | ||||||
| @@ -208,7 +220,7 @@ | |||||||
|   ports-margins: (:), |   ports-margins: (:), | ||||||
|   fill: none, |   fill: none, | ||||||
|   stroke: black + 1pt, |   stroke: black + 1pt, | ||||||
|   id: "", |   id: auto, | ||||||
|   auto-ports: true, |   auto-ports: true, | ||||||
|   ports-y: (:), |   ports-y: (:), | ||||||
|   debug: ( |   debug: ( | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user