Skip to content

Commit

Permalink
8/12 - Tag Linkage Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Allockse committed Aug 13, 2013
1 parent f242bd3 commit a9f77c3
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 44 deletions.
100 changes: 92 additions & 8 deletions DbMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Drawing;
using System.IO;
using System.Text;
using System.Collections.Generic;

// OleDB Database methods

Expand All @@ -19,7 +20,7 @@ public static class DbMethods
public static DataTable GetDbTable(string dbPath, string tableName)
{
// Create the return table
var returnTable = new DataTable(tableName);
DataTable returnTable;

// Set the initial connection type
var conType = ConnectionType.Ace;
Expand All @@ -34,12 +35,19 @@ public static DataTable GetDbTable(string dbPath, string tableName)
// Open the connection
connection.Open();

// Get the table schema
returnTable = new DataTable(tableName);

// Build the select command
var selectCommand = GetSelectCommand(tableName, connection);
selectCommand.CommandType = CommandType.TableDirect;

// Create the data adapter
var adapter = new OleDbDataAdapter(selectCommand);

// Get the table schema
adapter.FillSchema(returnTable, SchemaType.Mapped);

// Fill the return table
adapter.Fill(returnTable);
}
Expand Down Expand Up @@ -129,7 +137,6 @@ public static bool UpdateDbTable(string dbPath, DataTable inputTable)
{
return UpdateDbTable(dbPath, string.Empty, inputTable);
}

public static bool UpdateDbTable(string dbPath, string rowFilter, DataTable inputTable)
{
// Create the connection
Expand Down Expand Up @@ -160,6 +167,36 @@ public static bool UpdateDbTable(string dbPath, string rowFilter, DataTable inpu
// Return true on success
return true;
}
public static bool UpdateDbTable(string dbPath, DataRow row)
{
// Create the connection
using (var connection = GetConnection(dbPath))
{
try
{
// Open the connection
connection.Open();

// Create the adapter
var adapter = new OleDbDataAdapter();

// Create the update command
adapter.UpdateCommand = GetUpdateCommand(row, connection);

// Update the MDB table
var rowCount = adapter.Update(new DataRow[] { row });

} // Return false on error
catch (OleDbException ex)
{
throw ex;
//return false;
}
}

// Return true on success
return true;
}

public static bool DeleteDbValue(string dbpath, string tablename, string fieldname)
{
Expand Down Expand Up @@ -247,23 +284,70 @@ private static OleDbCommand GetUpdateCommand(DataTable inputTable, string rowFil

// Create the column parameter
var par = new OleDbParameter
{
ParameterName = "@" + col.ColumnName,
OleDbType = GetOleDbType(col.DataType),
Size = col.MaxLength,
SourceColumn = col.ColumnName,
};

// Add the parameter to the return command
retCommand.Parameters.Add(par);
}

// Remove the last comma
sb.Remove(sb.ToString().LastIndexOf(','), 1);

// Add a where clause if a rowfilter was provided
if (rowFilter != string.Empty)
sb.AppendFormat("WHERE {0}", rowFilter);

// Set the command text
retCommand.CommandText = sb.ToString();

// Return the command
return retCommand;
}
private static OleDbCommand GetUpdateCommand(DataRow row, OleDbConnection connection)
{
// Create the return command
var retCommand = connection.CreateCommand();

// Get the parent table
var parentTable = row.Table;

// Get the primary Key
var pKey = parentTable.PrimaryKey[0];

// Build the command string
var sb = new StringBuilder(string.Format("UPDATE {0} SET ", parentTable.TableName));

foreach (DataColumn col in parentTable.Columns)
{
if (!col.Unique)
{
// Append the command text
sb.AppendFormat("{0} = ?, ", col.ColumnName);

// Create the column parameter
var par = new OleDbParameter
{
ParameterName = col.ColumnName,
OleDbType = GetOleDbType(col.DataType),
Size = col.MaxLength,
SourceColumn = col.ColumnName,
};

// Add the parameter to the return command
retCommand.Parameters.Add(par);

// Add the parameter to the return command
retCommand.Parameters.Add(par);
}
}

// Remove the last comma
sb.Remove(sb.ToString().LastIndexOf(','), 1);

// Add a where clause if a rowfilter was provided
if (rowFilter != string.Empty)
sb.AppendFormat("WHERE {0}", rowFilter);
// Add a where clause to the primary key
sb.AppendFormat("WHERE {0} = {1}", pKey.ColumnName, row[pKey.ColumnName]);

// Set the command text
retCommand.CommandText = sb.ToString();
Expand Down
88 changes: 64 additions & 24 deletions SharpPlantReview/SprApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,56 @@ public double GlobalOptionsGet(int option)
return returnVal;
}

/// <summary>
/// Highlights a specified object in the main SmartPlant Review application Window
/// </summary>
/// <param name="objectId">Object Id of the of the entity to be highlighted.</param>
public void HighlightObject(int objectId)
{
// Highlight the object in SPR
int sprResult = DrApi.HighlightObject(objectId, 1, 0);

// Handle the errors
switch (sprResult)
{
case SprConstants.SprErrorNoApi:
throw SprExceptions.SprNotConnected;
case SprConstants.SprErrorInvalidObjectId:
throw SprExceptions.SprInvalidObjectId;
case SprConstants.SprErrorInvalidViewMask:
throw SprExceptions.SprInvalidView;
}
}

/// <summary>
/// Clears all highlighting from the main SmartPlant Review application window.
/// </summary>
public void HighlightClear()
{
// Clear the highlighting in SPR
int sprResult = DrApi.HighlightExit(1);

// Handle the errors
switch (sprResult)
{
case SprConstants.SprErrorNoApi:
throw SprExceptions.SprNotConnected;
case SprConstants.SprErrorInvalidViewMask:
throw SprExceptions.SprInvalidView;
}

// Refresh the main window
sprResult = DrApi.ViewUpdate(1);

// Handle the errors
switch (sprResult)
{
case SprConstants.SprErrorNoApi:
throw SprExceptions.SprNotConnected;
}

}

#endregion

#region Point Select
Expand Down Expand Up @@ -1148,9 +1198,6 @@ public void Tags_Place(ref SprTag tag)
// Throw an exception if not connected
if (!IsConnected) throw SprExceptions.SprNotConnected;

// Create the params
dynamic tagKey = Activator.CreateInstance(SprImportedTypes.DrKey);

// Create the origin point
var tagOrigin = new SprPoint3D();

Expand All @@ -1177,12 +1224,19 @@ public void Tags_Place(ref SprTag tag)
// Throw an exception if either of the point retrievals failed
if (objId == 0 || tagLeader == null) throw SprExceptions.SprNullPoint;

// Get the current object for the label key
var currentObject = GetObjectData(objId);
dynamic tagLabelKey = currentObject.DrObjectDataDbl.LabelKey;

// Turn label tracking on on the flag bitmask
tag.Flags |= SprConstants.SprTagLabel;

// Set the tag registry values
SprUtilities.SetTagRegistry(tag);

// Place the tag
int sprResult = DrApi.TagSetDbl(tag.Id, 0, tag.Flags, ref tagLeader.DrPointDbl,
ref tagOrigin.DrPointDbl, tagKey, tag.Text);
ref tagOrigin.DrPointDbl, tagLabelKey, tag.Text);

// Handle the errors
switch (sprResult)
Expand All @@ -1203,15 +1257,6 @@ public void Tags_Place(ref SprTag tag)
// Retrieve the placed tag data
tag = Tags_Get(tag.Id);

// Add an ObjectId field
Tags_AddDataField("object_id");

// Save the ObjectId to the tag data
tag.Data["object_id"] = objId;

// Update the tag
Tags_Update(tag);

// Clear the tag registry
SprUtilities.ClearTagRegistry();

Expand Down Expand Up @@ -1273,18 +1318,19 @@ public void Tags_EditLeader(ref SprTag tag)
// Throw an exception if either of the point retrievals failed
if (objId == 0 || tagLeader == null) throw SprExceptions.SprNullPoint;

// Create DrKey object
dynamic tagKey = Activator.CreateInstance(SprImportedTypes.DrKey);
// Get the current object for the label key
var currentObject = GetObjectData(objId);
dynamic tagLabelKey = currentObject.DrObjectDataDbl.LabelKey;

// Throw an exception if the key is null
if (tagKey == null) throw SprExceptions.SprObjectCreateFail;
// Turn label tracking on on the flag bitmask
tag.Flags |= SprConstants.SprTagLabel;

// Set the edit flag on the existing tag
tag.Flags |= SprConstants.SprTagEdit;

// Update the tag with the new leader points
int sprResult = DrApi.TagSetDbl(tag.Id, 0, tag.Flags, tagLeader.DrPointDbl,
tagOrigin.DrPointDbl, tagKey, tagText);
tagOrigin.DrPointDbl, tagLabelKey, tagText);

// Handle the errors
switch (sprResult)
Expand All @@ -1311,12 +1357,6 @@ public void Tags_EditLeader(ref SprTag tag)
tag.LeaderPoint = newLeader;
tag.OriginPoint = newOrigin;

// Add an ObjectId field
Tags_AddDataField("object_id");

// Save the ObjectId in the tag LabelKey spot
tag.Data["object_id"] = objId;

// Update the tag
Tags_Update(tag);

Expand Down
1 change: 1 addition & 0 deletions SharpPlantReview/SprConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static class SprConstants
public const int SprErrorInvalidTag = 16;
public const int SprErrorOutOfMemory = 19;
public const int SprErrorApiOutOfMemory = 20;
public const int SprErrorInvalidViewMask = 22;
public const int SprErrorInvalidObjectId = 23;
public const int SprErrorInvalidFileName = 29;
public const int SprErrorDirectoryWriteFailure = 30;
Expand Down
Loading

0 comments on commit a9f77c3

Please sign in to comment.