Files
stgsharp/内核源代码/StgSharp.Modeling/STEP/STEPDataReader.cs
T

154 lines
6.7 KiB
C#

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="STEPDataReader.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Modeling.STEP
{
public partial class STEPReader
{
private static Regex _expressionSplitterWithID = new Regex(
@"^#(?<ID>[0-9]+)\s*=\s*(?<CALLER>[A-Z_]+)\s?\(\s?(?<PARAMS>.+?)\s*\)\s*;",
RegexOptions.Compiled );
private ConcurrentDictionary<int, STEPExpression> cache = new ConcurrentDictionary<int, STEPExpression>(
);
private int _readCount = 0, _analyseCount = 0;
public Task AnalyseDataSection( ConcurrentQueue<STEPExpression> index )
{
return Task.Run(
() => {
if( _analyseCount == 1 ) {
throw new InvalidOperationException(
"Cannot read a STEP file while an other thread is reading" );
}
Interlocked.Exchange( ref _analyseCount, 1 );
STEPExpression exp = null;
while( exp != null && exp.Caller != "ENDSEC" ) {
if( index.TryDequeue( out exp ) ) {
//TODO 写一个Token向几何构造器的转换方法
}
}
;
Interlocked.Exchange( ref _analyseCount, 0 );
} );
}
public Task ReadDataSection( out ConcurrentQueue<STEPExpression> queue )
{
ConcurrentQueue<STEPExpression> expressionQueue = new ConcurrentQueue<STEPExpression>(
);
queue = expressionQueue;
return Task.Run(
() => {
if( _readCount == 1 ) {
throw new InvalidOperationException(
"Cannot read a STEP file while an other thread is reading" );
}
Interlocked.Exchange( ref _readCount, 1 );
using( MemoryMappedViewStream ms = _memoryFile.CreateViewStream(
0, _size, MemoryMappedFileAccess.Read ) ) {
using( StreamReader reader = new StreamReader( ms ) ) {
string line = string.Empty;
long pos = _dataStart;
reader.BaseStream.Seek( pos, SeekOrigin.Begin );
do {
line = ReadToken( reader, ref pos );
if( line == "DATA;" ) {
continue;
}
if( line == "ENDSEC;" ) {
expressionQueue.Enqueue(
new STEPExpression(
-1, "ENDSEC", string.Empty ) );
break;
}
Match match = _expressionSplitterWithID.Match(
line );
if( match.Success ) {
int id = int.Parse(
match.Groups[ "ID" ].Value );
STEPExpression exp = new STEPExpression(
id, match.Groups[ "CALLER" ].Value,
match.Groups[ "PARAMS" ].Value );
AddExpressionToCache(id, exp);
expressionQueue.Enqueue( exp );
}
} while (true);
}
}
Interlocked.Exchange( ref _readCount, 0 );
} );
}
private void AddExpressionToCache( int id, STEPExpression expression )
{
if( expression == null ) {
throw new ArgumentNullException( nameof( expression ) );
}
if( cache.TryGetValue( id,
out STEPExpression exp ) && exp.Caller !=
STEPExpression.TempCallerName ) {
throw new InvalidOperationException(
"Cannot add an expression to cache with the same id" );
}
cache.AddOrUpdate( id, expression, ( _, _ ) => expression );
}
private STEPExpression GetExpressionFromCache( int id )
{
if( cache.TryGetValue( id, out STEPExpression expression ) ) {
return expression;
}
STEPExpression tempCaller = STEPExpression.CreateTempExpression(id);
cache.AddOrUpdate(id, tempCaller, (_, newExpression) =>
{
tempCaller = newExpression;
return newExpression;
});
return tempCaller;
}
}
}