/// <summary> /// Returns all the speckle stream states present in the file. /// </summary> /// <param name="schema"></param> /// <returns></returns> public static List <StreamState> ReadState(DgnFile file) { var states = new List <StreamState>(); try { FindInstancesScope scope = FindInstancesScope.CreateScope(file, new FindInstancesScopeOption(DgnECHostType.All)); var schema = (ECSchema)DgnECManager.Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest); if (schema == null) { return(states); } ECQuery readWidget = new ECQuery(schema.GetClass(className)); readWidget.SelectClause.SelectAllProperties = true; using (DgnECInstanceCollection ecInstances = DgnECManager.Manager.FindInstances(scope, readWidget)) { var streamStatesInstance = ecInstances.First(); if (streamStatesInstance != null) { var str = streamStatesInstance[propertyName].StringValue; states = JsonConvert.DeserializeObject <List <StreamState> >(str); } } return(states); } catch (Exception e) { return(new List <StreamState>()); } }
/// <summary> /// Writes the stream states to the current schema. /// </summary> /// <param name="streamStates"></param> public static void WriteStreamStateList(DgnFile File, List <StreamState> streamStates) { DgnECManager Manager = DgnECManager.Manager; FindInstancesScope scope = FindInstancesScope.CreateScope(File, new FindInstancesScopeOption(DgnECHostType.All)); IECSchema schema = RetrieveSchema(File, scope); IECClass ecClass = schema.GetClass(className); ECQuery readWidget = new ECQuery(ecClass); readWidget.SelectClause.SelectAllProperties = true; using (DgnECInstanceCollection instances = Manager.FindInstances(scope, readWidget)) { foreach (IDgnECInstance instance in instances) { instance.Delete(); } } DgnECInstanceEnabler instanceEnabler = Manager.ObtainInstanceEnabler(File, ecClass); var data = JsonConvert.SerializeObject(streamStates) as string; StandaloneECDInstance _instance = instanceEnabler.SharedWipInstance; _instance.SetAsString(propertyName, data); instanceEnabler.CreateInstanceOnFile(File, _instance); }
private static ECSchema RetrieveSchema(DgnFile File, FindInstancesScope scope) { DgnECManager Manager = DgnECManager.Manager; DgnModel model = Session.Instance.GetActiveDgnModel(); var schemas = (List <string>)Manager.DiscoverSchemasForModel(model, ReferencedModelScopeOption.All, false); var schemaString = schemas.Where(x => x.Contains(schemaName)).FirstOrDefault(); if (schemaString != null) { try { IECSchema schema = Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest); return((ECSchema)schema); } catch (Exception e) { return(null); } } else { return(CreateSchema(File)); } }
public static ECSchema RetrieveSchema() { DgnFile File = Session.Instance.GetActiveDgnFile(); DgnECManager Manager = DgnECManager.Manager; FindInstancesScope scope = FindInstancesScope.CreateScope(File, new FindInstancesScopeOption(DgnECHostType.All)); IECSchema schema = Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest); return((ECSchema)schema); }
public static List <CellHeaderElement> FindSPDSElementsByInstance(DgnModel model) { if (model == null) { throw new ArgumentNullException(); } DgnFile file = model.GetDgnFile(); DgnFileOwner fileOwner = new DgnFileOwner(file); IECSchema Schema = null; { // todo Правильная инициализация схемы: Schema = DgnECManager.Manager?.LocateDeliveredSchema( ECHelper.SCHEMA_NAME, VrnMajor, VrnMinor, SchemaMatchType.LatestCompatible, file); } if (Schema == null) { return(null); } IECClass commonClass = Schema.GetClass(ECCLASS_COMMON_NAME); ECQuery query = new ECQuery(commonClass); var modelConn = DgnModelRepositoryConnection.CreateConnection( new DgnECConnectionOptions(), fileOwner, model); var instColl = DgnECManager.Manager.FindDgnECInstances(modelConn, query); if (instColl == null) { return(null); } List <CellHeaderElement> resList = new List <CellHeaderElement>(); foreach (var item in instColl) { Element element; if (DgnECManager.TryGetElementInfo(modelConn, item.InstanceId, out element)) { if (element is CellHeaderElement) { resList.Add(element as CellHeaderElement); } } } return(resList); }
public static DgnECInstanceCollection RetrieveInstances() { DgnFile File = Session.Instance.GetActiveDgnFile(); DgnECManager Manager = DgnECManager.Manager; FindInstancesScope scope = FindInstancesScope.CreateScope(File, new FindInstancesScopeOption(DgnECHostType.All)); IECSchema schema = Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest); IECClass ecClass = schema.GetClass(className); ECQuery readWidget = new ECQuery(ecClass); readWidget.SelectClause.SelectAllProperties = true; DgnECInstanceCollection instances = Manager.FindInstances(scope, readWidget); return(instances); }
public static IECSchema GetSchema(DgnFile file) { if (file == null) { throw new ArgumentNullException(); } if (file == cachedFile && ECHelper.cachedSchema != null) { return(ECHelper.cachedSchema); } cachedSchema = null; cachedFile = file; { // todo Правильная инициализация схемы: // в.1 cachedSchema = DgnECManager.Manager.LocateDeliveredSchema(SCHEMA_NAME, VrnMajor, VrnMinor, SchemaMatchType.LatestCompatible, file); // в.2 //FindInstancesScope scope = FindInstancesScope.CreateScope(file, new FindInstancesScopeOption()); //Schema = DgnECManager.Manager.LocateSchemaInScope(scope, "SPDSTools", 1, 0, SchemaMatchType.LatestCompatible); } if (cachedSchema == null) { return(null); } IEnumerable <string> schemas = DgnECManager.Manager.DiscoverSchemas( file, ReferencedModelScopeOption.All, true); if (!schemas.Contains(cachedSchema.FullName)) { // схема не подготовлена для использования в файле: SchemaImportStatus impStat = DgnECManager.Manager.ImportSchema( cachedSchema, file, new ImportSchemaOptions( isExternal: true, importReferencedSchemas: false, registerHighPriorityLocater: true)); if (impStat != SchemaImportStatus.Success) { // todo ! throw new AddinException("Failed to initialize ECSchema: {0}", SchemaFullName); } } return(cachedSchema); }
private static ECSchema CreateSchema(DgnFile File) { ECSchema newSchema = new ECSchema(schemaName, 1, 0, schemaName); ECClass streamStateClass = new ECClass(className); ECProperty streamDataProp = new ECProperty(propertyName, ECObjects.StringType); streamStateClass.Add(streamDataProp); newSchema.AddClass(streamStateClass); var status = DgnECManager.Manager.ImportSchema(newSchema, File, new ImportSchemaOptions()); if (status != SchemaImportStatus.Success) { return(null); } return(newSchema); }
public static IDgnECInstance RetrieveInstance(StreamState streamState) { DgnFile File = Session.Instance.GetActiveDgnFile(); DgnECManager Manager = DgnECManager.Manager; FindInstancesScope scope = FindInstancesScope.CreateScope(File, new FindInstancesScopeOption(DgnECHostType.All)); IECSchema schema = Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest); IECClass ecClass = schema.GetClass(className); ECQuery readWidget = new ECQuery(ecClass); readWidget.SelectClause.SelectAllProperties = true; DgnECInstanceCollection instances = Manager.FindInstances(scope, readWidget); var instance = instances.Where(x => x["Id"].StringValue == streamState.Stream.id).FirstOrDefault(); return(instance); }
public NodeUpdateResult WriteItems ( NodeUpdateContext updateContext, [DgnModelProvider, Replicatable] object ElementsToWriteTo, [Replicatable] string ItemTypeLibraryName, [Replicatable] string ItemTypeName, [Replicatable(-1, true)] string[] PropertyName, [Replicatable(-1, true)] string[] PropertyValue ) { try { DgnFile dgnFile = Session.Instance.GetActiveDgnFile(); DgnModel dgnModel = Session.Instance.GetActiveDgnModel(); Feature feat = (Feature)ElementsToWriteTo; long eleID = feat.Element.ID; Bentley.DgnPlatformNET.Elements.Element ele = dgnModel.FindElementById(new ElementId(ref eleID)); ItemTypeLibrary itl = ItemTypeLibrary.FindByName(ItemTypeLibraryName, dgnFile); ItemType itemType = itl.GetItemTypeByName(ItemTypeName); CustomItemHost customItemHost = new CustomItemHost(ele, false); IDgnECInstance ecInstance = customItemHost.GetCustomItem(ItemTypeLibraryName, ItemTypeName); if (ecInstance == null) { ecInstance = customItemHost.ApplyCustomItem(itemType); } for (int i = 0; i < PropertyName.Length; i++) { ecInstance.SetString(PropertyName[i].ToString(), PropertyValue[i].ToString()); } ecInstance.WriteChanges(); } catch (Exception ex) { return(new NodeUpdateResult.TechniqueException(ex)); } return(NodeUpdateResult.Success); }
/// <summary> /// Writes the stream states to the current schema. /// </summary> /// <param name="streamStates"></param> public static void WriteStreamStateList(List <StreamState> streamStates) { DgnFile File = Session.Instance.GetActiveDgnFile(); DgnECManager Manager = DgnECManager.Manager; FindInstancesScope scope = FindInstancesScope.CreateScope(File, new FindInstancesScopeOption(DgnECHostType.All)); IECSchema schema = Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.Latest); if (schema == null) { schema = StreamStateListSchema.GetSchema(); } IECClass ecClass = schema.GetClass(className); ECQuery readWidget = new ECQuery(ecClass); readWidget.SelectClause.SelectAllProperties = true; using (DgnECInstanceCollection instances = Manager.FindInstances(scope, readWidget)) { foreach (IDgnECInstance instance in instances) { instance.Delete(); } } DgnECInstanceEnabler instanceEnabler = Manager.ObtainInstanceEnabler(File, ecClass); foreach (var streamState in streamStates) { var data = JsonConvert.SerializeObject(streamState) as string; StandaloneECDInstance instance = instanceEnabler.SharedWipInstance; instance.SetAsString("Id", streamState.Stream.id); instance.SetAsString("StreamData", data); instanceEnabler.CreateInstanceOnFile(File, instance); } }
public static ECSchema AddSchema() { DgnFile File = Session.Instance.GetActiveDgnFile(); DgnECManager Manager = DgnECManager.Manager; ECSchema newSchema = new ECSchema(schemaName, 1, 0, schemaName); ECClass streamStateClass = new ECClass(className); ECProperty streamIdProp = new ECProperty("Id", ECObjects.StringType); ECProperty streamDataProp = new ECProperty("StreamData", ECObjects.StringType); streamStateClass.Add(streamIdProp); streamStateClass.Add(streamDataProp); newSchema.AddClass(streamStateClass); var status = Manager.ImportSchema(newSchema, File, new ImportSchemaOptions()); if (status != SchemaImportStatus.Success) { return(null); } return(newSchema); }
/// <summary> /// Returns all the speckle stream states present in the custom schema (schema is attached to file). /// </summary> /// <param name="schema"></param> /// <returns></returns> public static List <StreamState> ReadState(ECSchema schema) { DgnFile File = Session.Instance.GetActiveDgnFile(); DgnECManager Manager = DgnECManager.Manager; try { ECQuery readWidget = new ECQuery(schema.GetClass(className)); readWidget.SelectClause.SelectAllProperties = true; FindInstancesScope scope = FindInstancesScope.CreateScope(File, new FindInstancesScopeOption(DgnECHostType.All)); var states = new List <StreamState>(); using (DgnECInstanceCollection ecInstances = Manager.FindInstances(scope, readWidget)) { foreach (IDgnECInstance instance in ecInstances) { var id = instance["Id"].StringValue; var streamStateData = instance["StreamData"].StringValue; var state = JsonConvert.DeserializeObject <StreamState>(streamStateData); states.Add(state); } } if (states != null) { states.ForEach(x => x.Initialise(true)); } return(states); } catch (Exception e) { return(new List <StreamState>()); } }
public static SchemaImportStatus ImportSchema(ECSchema schema, DgnFile file) { return(DgnECManager.Manager.ImportSchema(schema, file, new ImportSchemaOptions())); }
public NodeUpdateResult PlaceParametricCell ( NodeUpdateContext updateContext, [Replicatable, DgnModelProvider] IPoint PlacementPoint, [Replicatable] IPlane PlacementPlane, [In] string CellLibraryPath, [In] string CellDefinitionName, [In] string CellVariation, [Replicatable(-1, true)] string[] CellVariableNames, [Replicatable(-1, true)] string[] CellVariableValues ) { this.ClearAndErase(updateContext); // Remove old feature if (this.ReplicationIndex == 0 && CellLibraryPath == null || CellDefinitionName == null) { return new NodeUpdateResult.IncompleteInputs(CellLibraryPath, CellDefinitionName); } else { //NEED TO IMPLEMENT A SHARED CELL UPDATE IF UpdateSharedCell == true; THEN ONCE UPDATED SET THE PARAMETER TO FALSE SO THAT THE NEXT PLACED CELL DOESN'T UPDATE THE SHARED CELL AGAIN //Check if cell library is attached and if not attach it if (this.ReplicationIndex == 0 && MSApp.IsCellLibraryAttached == false || MSApp.AttachedCellLibrary.FullName != CellLibraryPath) { MSApp.AttachCellLibrary(CellLibraryPath); } DgnFile activeDgnFile = Session.Instance.GetActiveDgnFile(); DgnModel activeModel = Session.Instance.GetActiveDgnModel(); DgnModel cellModel = null; ParametricCellElement pCell = null; ParametricCellDefinitionElement cellDef = ParametricCellDefinitionElement.FindByName(CellDefinitionName, activeDgnFile); if (cellDef == null) //cell not in active file, load from attached cell library { var opts = CellLibraryOptions.Include3d | CellLibraryOptions.IncludeAllLibraries | CellLibraryOptions.IncludeParametric; var libs = new CellLibraryCollection(opts); foreach (var lib in libs) { if (CellDefinitionName.Equals(lib.Name)) { StatusInt status; cellModel = lib.File.LoadRootModelById(out status, lib.File.FindModelIdByName(lib.Name), true, false, true); break; } } if (null == cellModel) //Cell definition (model) doesn't exist in the cell model file { LsBuilder lsBuilder = new LsBuilder(); Ls ls = lsBuilder.AppendLineLiteral("Error loading cell definition. Check cell definition name and library are correct.").ToLs(); return new NodeUpdateResult.TechniqueFailureMessage(ls); } else { var hdlr = DgnComponentDefinitionHandler.GetForModel(cellModel); var status = hdlr.DefinitionModelHandler.CreateCellDefinition(activeDgnFile); if (ParameterStatus.Success == status) cellDef = ParametricCellDefinitionElement.FindByName(CellDefinitionName, activeDgnFile); else { LsBuilder lsBuilder = new LsBuilder(); Ls ls = lsBuilder.AppendLineLiteral("Error creating cell definition in active file.").ToLs(); return new NodeUpdateResult.TechniqueFailureMessage(ls); } } } try { pCell = ParametricCellElement.Create(cellDef, CellVariation, activeModel); //Cell origin point - adjusted for U.O.R. double uor = MSApp.ActiveModelReference.UORsPerMasterUnit; DPoint3d cellOrigin = DPoint3d.Multiply(uor, PlacementPoint.GetDPoint3d()); pCell.Origin = cellOrigin; DTransform3d dTransform3D = PlacementPlane.GetDTransform3d(); DPlane3d dPlane3D = PlacementPlane.GetDPlane3d(); DMatrix3d dMatrix3D = new DMatrix3d(dTransform3D); pCell.Rotation = dMatrix3D; //pCell.IsInvisible = true; //We don't want multiple elements visible pCell.AddToModel(); //Add to the model so we can assign variables and retrieve the element //Assign custom variables if they exist if (CellVariableNames != null && CellVariableValues != null) { IECClass ecClass = pCell.Parameters.ClassDefinition; IDgnECInstance eci = DgnECManager.FindECInstanceOnElement(pCell, ecClass); for (int i = 0; i < CellVariableNames.Length; ++i) { IEnumerator<IECPropertyValue> props = eci.GetEnumerator(false, true); while (props.MoveNext()) { if (props.Current.Property.DisplayLabel == CellVariableNames[i].ToString()) { props.Current.StringValue = CellVariableValues[i].ToString(); } } } eci.WriteChanges(); } IElement ele = MSApp.ActiveModelReference.GetLastValidGraphicalElement(); SetElement(ele); //Commit as a GC feature pCell.Dispose(); //Clean up the memory of pCell, only 'ele' will be retained } catch (Exception ex) { return new NodeUpdateResult.TechniqueException(ex); } return NodeUpdateResult.Success; } }