我正在撰寫一個類別庫來從名為 SpaceClaim 的 CAD 軟體中的 3D 模型中提取資料。當我從模型中提取資料時,我得到一個字串,以下 3 種可用的可能性:
{Cone: Origin = (0.006, -0.006, 0.01), Direction = (0, 0, 1), Radius = 0.008, HalfAngle = 0.785398163397448}
{Cylinder: Origin = (-0.003, 0.001, 0), Direction = (0, 0, 1), Radius = 0.00921954445729289}
{Plane: Origin = (0, 0, 0.01), DirX = (1, 0, 0), DirY = (0, 1, 0), DirZ = (0, 0, 1)}
所以首先我們有型別(Plane、Cone 或 Cilinder),然后 Origin 是一個看起來很奇怪的陣列(用 [] 替換 ()),現在我只需要 Radius(如果 Type = Cone 或 Cilinder)和 HalfAngle (如果型別 = 圓錐)
我做了一個名為 FaceInfo 的類:
public class FaceInfo
{
public string Type { get; set; }
public Coordinates Origin { get; set; }
public double Radius { get; set; } = 0;
public double HalfAngle { get; set; } = 0;
}
public class Coordinates
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Coordinates(string value)
{
string [] split = value.Split(new char[] { '(', ')', ',' });
X = double.Parse(split[0]);
Y = double.Parse(split[1]);
Z = double.Parse(split[2]);
}
}
我試過替換字符、拆分、子字串。
在某些時候一切都失敗了。
請幫忙。謝謝
PS。坐標類仍然需要 () 作為陣列,也需要修復它。但首先,回傳的字串。如果資料不可讀,則使用 () 或 [] 毫無意義。
uj5u.com熱心網友回復:
要提取資料,您可以使用以下解決方案。
string pattern = @"\((?<origin>.*)\). =\s*\((?<direction>.*)\). =\s*(?<radius>[\d\.]*). =\s*(?<halfAngle>.*)\}"; // continue the pattern for your needs
Regex rx = new Regex(pattern);
Match m = rx.Match(searchString);
FaceInfo faceInfo = new FaceInfo();
if (m.Success)
{
faceInfo.Origin = new Coordinates(m.Groups["origin"].Value);
faceInfo.Direction = new Coordinates(m.Groups["direction"].Value);
faceInfo.Radius = Convert.ToDouble(m.Groups["radius"].Value);
faceInfo.HalfAngle = Convert.ToDouble(m.Groups["halfAngle"].Value);
}
并將坐標類修改為
public class Coordinates
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Coordinates(string value)
{
string[] split = value.Split(new char[] { ',' });
X = double.Parse(split[0]);
Y = double.Parse(split[1]);
Z = double.Parse(split[2]);
}
}
uj5u.com熱心網友回復:
這聽起來像是一個決議任務。所以我會去(偽代碼,所以請檢查我)
var lines = input.Split("\n");
Console.WriteLine(lines[0]); // for QA
foreach(var line in lines) {
int index1= line.indexOf(":");
string _type = line.SubString(1,index1);
Console.WriteLine(_type); // for QA;
string restOfLine = line.SubString(index1 1, line.Length - index1);
Console.WriteLine(restOfLine); // for QA;
var parts = restOfLine.Split(',');
foreach(var p in parts) {
Console.WriteLine(p); // for QA;
// ... continue the parsing....
}
FaceInfo f = new FaceInfo();
f.Type = _type;
// f. .... whatever = whatever...
}
類似的東西?確保以非常正確的方式進行決議,因為此文本并非完全采用標準 JSON 格式......這就是所有// for QA評論的目的。
uj5u.com熱心網友回復:
因此,讓字串值是您從 CAD 程式中獲得的輸入字串。您已經放置了示例輸入,所以我也會使用它們。
{Cone: Origin = (0.006, -0.006, 0.01)
,
Direction = (0, 0, 1), Radius = 0.008
,
HalfAngle = 0.785398163397448}
當您使用 split 時
new char[] { '(' , ')' , ',' }
每當看到 '(' , ')' 或 ',' 時它都會決議字串
所以你的 split 方法的輸出陣列看起來像
0 => 圓錐:原點
1 => 0.006
2 => -0.006
3 => 0.01
...
要查看陣列的元素,您可以使用
簡單的方法:
foreach(string element in splitOutputArray)
{
// If you use form
MessageBox.Show(element);
// If you use console
Console.WriteLine(element);
}
更正確的方法:使用斷點。 https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2022
因此,當您查看陣列的元素時,您會注意到問題所在。您的索引不正確。
可能的解決方案:
您的欄位 (Origin,Direction...) 和欄位中的值 (0.006, -0.006, 0.01) 均以“,”分隔。因此,您必須查看輸出的元素并更正索引。
X = double.Parse(split[0]);
Y = double.Parse(split[1]);
Z = double.Parse(split[2]);
一定是這樣的
X = double.Parse(split[1]);
Y = double.Parse(split[2]);
Z = double.Parse(split[3]);
總結:您做的大部分作業都是正確的,但您必須更正索引 (split[index])。對不起,如果我過度解釋我不知道你的水平。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/450972.html
