public override void Execute(ScriptState state) { string path = state.ResolvePath(File); state.Log.Status($"Exporting to {path}"); FileTypeInfo fti = ForceType; if (ForceType == null) { var ext = System.IO.Path.GetExtension(path); if (!FileTypeInfo.TryParseName(ext, out fti)) { throw new Exception($"Unrecognised file extension \"{ext}\". Use a conventional extension or specify the type explicitly."); } } if (!fti.CanExport) { throw new Exception($"Cannot export {fti.FormatName}"); } fti.Export(state.Data, path); }
public override void Execute(ScriptState state) { var filepath = state.ResolvePath(File); state.Log.Status($"Importing from {filepath}"); FileTypeInfo effectiveType = null; if (ForceType != null) { effectiveType = ForceType; } else { var ext = System.IO.Path.GetExtension(filepath); if (!FileTypeInfo.TryParseName(ext, out effectiveType)) { throw new Exception($"Unrecognised file extension \"{ext}\". Use a conventional extension or specify the type explicitly."); } } if (!effectiveType.CanImport) { throw new Exception($"No import support for {effectiveType}"); } var parentObjects = new Dictionary <string, S.Object3D>(); foreach (var kv in Parents) { var parent = state.Data.SectionsOfType <S.Object3D>().FirstOrDefault(i => i.Name == kv.Value); if (parent == null) { throw new Exception($"Cannot find rootpoint element {kv.Value}"); } parentObjects.Add(kv.Key, parent); } S.Object3D defaultRootObject; if (DefaultRootPoint != null) { defaultRootObject = state.Data.SectionsOfType <S.Object3D>().FirstOrDefault(i => i.Name == DefaultRootPoint); } else { defaultRootObject = state.DefaultRootPoint; } var throwOnNoParent = effectiveType == FileTypeInfo.Obj; S.Object3D ParentFinder(string name) { if (parentObjects.ContainsKey(name)) { return(parentObjects[name]); } if (defaultRootObject == null && throwOnNoParent) { throw new Exception($"No default- nor object-rootpoint set for {name}"); } return(defaultRootObject); } Importers.IOptionReceiver opts = effectiveType.CreateOptionReceiver(); foreach (var kv in ImporterOptions) { opts.AddOption(kv.Key, kv.Value); } bool createObjects = CreateNewObjects ?? state.CreateNewObjects; effectiveType.Import(state.Data, filepath, createObjects, ParentFinder, opts); }
public override void ParseXml(XElement element) { this.File = ScriptXml.RequiredAttr(element, "file"); var strType = element.Attribute("type")?.Value; if (FileTypeInfo.TryParseName(strType, out var type)) { this.ForceType = type; } else { this.ForceType = null; } var strCreateObjects = element.Attribute("create_objects")?.Value; if (strCreateObjects != null && bool.TryParse(strCreateObjects, out var createObjects)) { this.CreateNewObjects = createObjects; } else if (strCreateObjects != null) { throw new Exception($"create_objects must be boolean, \"{bool.TrueString}\" or \"{bool.FalseString}\""); } foreach (var child in element.Elements()) { switch (child.Name.ToString()) { case "rootpoint": var targetname = ScriptXml.RequiredAttr(child, "name"); foreach (var rpitem in child.Elements()) { switch (rpitem.Name.ToString()) { case "object": var childName = ScriptXml.RequiredAttr(rpitem, "name"); if (this.Parents.ContainsKey(childName)) { throw new Exception($"Cannot redefine rootpoint for object {childName}"); } this.Parents.Add(childName, targetname); break; case "default": if (this.DefaultRootPoint == null) { this.DefaultRootPoint = targetname; } else { throw new Exception($"Cannot redefine default rootpoint to {targetname}"); } break; default: throw new Exception($"Invalid <rootpoint> child: {rpitem.Name}"); } } break; case "option": this.ImporterOptions.Add(ScriptXml.RequiredAttr(child, "name"), child.Value.Trim()); break; } } }