Skip to content

Commit 0416f3b

Browse files
authored
Database Areas + String Utils (#104)
1 parent a720840 commit 0416f3b

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Sources/VimKit/Database+Models.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ extension Database {
9090
/// Provides a static list of the indexed model types.
9191
/// See: https://github.com/vimaec/vim/blob/master/ObjectModel/object-model-schema.json
9292
static let models: [any IndexedPersistentModel.Type] = [
93+
AreaScheme.self,
94+
Area.self,
9395
AssemblyInstance.self,
9496
Asset.self,
9597
BimDocument.self,
@@ -146,6 +148,75 @@ extension Database {
146148
}
147149
}
148150

151+
@Model
152+
public final class AreaScheme: IndexedPersistentModel {
153+
154+
public static func predicate(_ index: Int64) -> Predicate<AreaScheme> {
155+
#Predicate<AreaScheme> { $0.index == index }
156+
}
157+
158+
@Transient
159+
public static let importPriority: ModelImportPriority = .normal
160+
161+
@Attribute(.unique)
162+
public var index: Int64
163+
public var isGrossBuildingArea: Bool
164+
public var element: Element?
165+
166+
/// Initializer.
167+
public required init() {
168+
index = .empty
169+
isGrossBuildingArea = false
170+
}
171+
172+
public func update(from data: [String: AnyHashable], cache: ImportCache) {
173+
isGrossBuildingArea = data["IsGrossBuildingArea"] as? Bool ?? false
174+
if let idx = data["Element"] as? Int64, idx != .empty {
175+
element = cache.findOrCreate(idx)
176+
}
177+
}
178+
}
179+
180+
@Model
181+
public final class Area: IndexedPersistentModel {
182+
183+
public static func predicate(_ index: Int64) -> Predicate<Area> {
184+
#Predicate<Area> { $0.index == index }
185+
}
186+
187+
@Transient
188+
public static let importPriority: ModelImportPriority = .normal
189+
190+
@Attribute(.unique)
191+
public var index: Int64
192+
public var isGrossInterior: Bool
193+
public var perimeter: Double
194+
public var value: Double
195+
public var scheme: AreaScheme?
196+
public var element: Element?
197+
public var number: String?
198+
199+
/// Initializer.
200+
public required init() {
201+
index = .empty
202+
isGrossInterior = false
203+
perimeter = 0
204+
value = 0
205+
}
206+
207+
public func update(from data: [String: AnyHashable], cache: ImportCache) {
208+
isGrossInterior = data["IsGrossInterior"] as? Bool ?? false
209+
perimeter = data["Perimeter"] as? Double ?? .zero
210+
value = data["Value"] as? Double ?? .zero
211+
number = data["Number"] as? String
212+
if let idx = data["AreaScheme"] as? Int64, idx != .empty {
213+
scheme = cache.findOrCreate(idx)
214+
}
215+
if let idx = data["Element"] as? Int64, idx != .empty {
216+
element = cache.findOrCreate(idx)
217+
}
218+
}
219+
}
149220

150221
@Model
151222
public final class Asset: IndexedPersistentModel {

Sources/VimKit/Extensions/String+Extensions.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,29 @@ public extension String {
1818
let hashed = SHA256.hash(data: Data(self.utf8))
1919
return hashed.compactMap { String(format: "%02x", $0) }.joined()
2020
}
21+
22+
/// Convenience var that trim all whitespace.
23+
var trimmed: String {
24+
trimmingCharacters(in: .whitespaces)
25+
}
26+
27+
/// Convenience subscript using a countable closed range.
28+
/// - Parameters:
29+
/// - bounds: the bounds to subscript
30+
/// - Returns: a string at the specified bounds
31+
subscript (bounds: CountableClosedRange<Int>) -> String {
32+
let start = index(startIndex, offsetBy: bounds.lowerBound)
33+
let end = index(startIndex, offsetBy: bounds.upperBound)
34+
return String(self[start...end])
35+
}
36+
37+
/// Convenience subscript using a countable range.
38+
/// - Parameters:
39+
/// - bounds: the bounds to subscript
40+
/// - Returns: a string at the specified bounds
41+
subscript (bounds: CountableRange<Int>) -> String {
42+
let start = index(startIndex, offsetBy: bounds.lowerBound)
43+
let end = index(startIndex, offsetBy: bounds.upperBound)
44+
return String(self[start..<end])
45+
}
2146
}

0 commit comments

Comments
 (0)