英文:
Add Properties to string
问题
这是您提供的代码的翻译部分:
它关闭了我的原始问题。对C#相当新,我想知道是否有一种方法可以实现以下操作:
string joint = "Press Joint" || "ProPress";
我是否可以使用switch语句来实现这一点?即将字符串设置为空,并以编程方式提取一个值,然后根据该值切换情况?我已经创建了一个更新程序实用程序,用于检查Revit中的模型,以强制执行最小管道长度标准。代码相当长,但希望我提取出来的内容有意义。
internal class UtilsFabricationPipeLengths
{
public static FailureDefinitionId illegalPipeLengthFailureId;
public static FailureDefinitionId illegalPipeLengthFailureId2;
public static FailureDefinitionId illegalPipeLengthFailureId3;
// public static FailureDefinitionId illegalPipeLengthFailureId4;
public static FailureDefinitionId illegalPipeLengthFailureId5;
public static FailureDefinitionId illegalPipeLengthFailureId6;
public static FailureDefinitionId illegalPipeLengthFailureId7;
public class FabricationPipeLengthsUpdater : IUpdater
{
private readonly UpdaterId fabricationPipeLengthsUpdaterId;
private FabricationDimensionDefinition fabricationDimensionDefinition;
public FabricationPipeLengthsUpdater(AddInId id)
{
fabricationPipeLengthsUpdaterId = new UpdaterId(id, new Guid("B0C85739-93DB-44A6-BC74-92280998063E"));
}
public void Execute(UpdaterData data)
{
try
{
{
string brazed = "Brazed";
string pressJoint = "Press Joint";
string soldered = "Soldered";
string threaded = "Threaded";
string buttWeld = "ButtWeld";
string pexJoint = "Pex Joint";
Document doc = data.GetDocument();
List<ElementId> ids = new List<ElementId>();
ids.AddRange(data.GetAddedElementIds());
ids.AddRange(data.GetModifiedElementIds());
foreach (ElementId id in ids)
{
if (doc.GetElement(id) is FabricationPart fabricationPart)
{
Autodesk.Revit.DB.Parameter lLength = fabricationPart.LookupParameter("Length");
lLength.AsDouble().ToString();
Autodesk.Revit.DB.Parameter fpdsd = fabricationPart.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
fpdsd.AsDouble().ToString();
// 不要执行如果更改不是由所需的参数触发的
if (!data.IsChangeTriggered(id, Element.GetChangeTypeParameter(new ElementId(BuiltInParameter.FABRICATION_PART_LENGTH))))
{
break;
}
if (!fabricationPart.IsValidObject)
continue;
// Press To Existing Soldered
if (fabricationPart.ProductInstallType == pressJoint &&
(decimal)lLength.AsDouble() < 0.2239583333m &&
fabricationPart.ProductSizeDescription == "1-1/4" ||
fabricationPart.ProductInstallType == pressJoint &&
(decimal)lLength.AsDouble() < 0.2812500000m &&
fabricationPart.ProductSizeDescription == "1-1/2" ||
fabricationPart.ProductInstallType == pressJoint &&
(decimal)lLength.AsDouble() < 0.3229166667m &&
fabricationPart.ProductSizeDescription == "2")
{
// 执行某些操作
}
else
{
continue;
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
public UpdaterId GetUpdaterId()
{
UpdaterId id = fabricationPipeLengthsUpdaterId;
return (id);
}
public ChangePriority GetChangePriority()
{
return ChangePriority.MEPSystems;
}
public string GetUpdaterName()
{
return "Fabrication Pipe Updater";
}
public string GetAdditionalInformation()
{
return "Fabrication Pipe updater example: checks the lengths of pipe to make sure " +
"all pipes meet minimum pipe length requirements.";
}
}
}
变量pressJoint
的字符串值可以在"Press Joint"、"ProPress"和"PressFit"之间变化。
英文:
It closed my original question. Fairly new to c# & I'm wondering if there is a way to do the following:
string joint = "Press Joint" || "ProPress";
Would I achieve this using a switch statement? I.e. set the string to be empty & programmatically pull a value & switch the case depending on the value? I've created an updater utility to check the models in Revit to enforce minimum pipe length standards. The code is pretty lengthy but hopefully what I pulled out makes sense.
internal class UtilsFabricationPipeLengths
{
public static FailureDefinitionId illegalPipeLengthFailureId;
public static FailureDefinitionId illegalPipeLengthFailureId2;
public static FailureDefinitionId illegalPipeLengthFailureId3;
// public static FailureDefinitionId illegalPipeLengthFailureId4;
public static FailureDefinitionId illegalPipeLengthFailureId5;
public static FailureDefinitionId illegalPipeLengthFailureId6;
public static FailureDefinitionId illegalPipeLengthFailureId7;
public class FabricationPipeLengthsUpdater : IUpdater
{
private readonly UpdaterId fabricationPipeLengthsUpdaterId;
private FabricationDimensionDefinition fabricationDimensionDefinition;
public FabricationPipeLengthsUpdater(AddInId id)
{
fabricationPipeLengthsUpdaterId = new UpdaterId(id, new Guid("B0C85739-93DB-44A6-BC74-92280998063E"));
}
public void Execute(UpdaterData data)
{
try
{
{
string brazed = "Brazed";
string pressJoint = "Press Joint";
string soldered = "Soldered";
string threaded = "Threaded";
string buttWeld = "ButtWeld";
string pexJoint = "Pex Joint";
Document doc = data.GetDocument();
List<ElementId> ids = new List<ElementId>();
ids.AddRange(data.GetAddedElementIds());
ids.AddRange(data.GetModifiedElementIds());
foreach (ElementId id in ids)
{
if (doc.GetElement(id) is FabricationPart fabricationPart)
{
Autodesk.Revit.DB.Parameter lLength = fabricationPart.LookupParameter("Length");
lLength.AsDouble().ToString();
Autodesk.Revit.DB.Parameter fpdsd = fabricationPart.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
fpdsd.AsDouble().ToString();
//Don't execute if the change was not trigerred by the wanted parameter
if (!data.IsChangeTriggered(id, Element.GetChangeTypeParameter(new ElementId(BuiltInParameter.FABRICATION_PART_LENGTH))))
{
break;
}
if (!fabricationPart.IsValidObject)
continue;
#region Press To Existing Soldered
if ( fabricationPart.ProductInstallType == pressJoint &&
(decimal)lLength.AsDouble() < 0.2239583333m &&
fabricationPart.ProductSizeDescription == "1-1/4" ||
fabricationPart.ProductInstallType == pressJoint &&
(decimal)lLength.AsDouble() < 0.2812500000m &&
fabricationPart.ProductSizeDescription == "1-1/2" ||
fabricationPart.ProductInstallType == pressJoint &&
(decimal)lLength.AsDouble() < 0.3229166667m &&
fabricationPart.ProductSizeDescription == "2")
#endregion
else
{
continue;
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
public UpdaterId GetUpdaterId()
{
UpdaterId id = fabricationPipeLengthsUpdaterId;
return (id);
}
public ChangePriority GetChangePriority()
{
return ChangePriority.MEPSystems;
}
public string GetUpdaterName()
{
return "Fabrication Pipe Updater";
}
public string GetAdditionalInformation()
{
return "Fabrication Pipe updater example: checks the lenghts of pipe to make sure " +
"all pipes meet minimum pipe length requirements.";
}
}
}
The string value pressJoint can vary with what is being checked between "Press Joint", "ProPress", & "PressFit".
答案1
得分: 3
No. A string is one value. If you want to test whether a string is something or something else - or one of some set of options, then:
- for constant strings, you can use
if (value is "abc" or "def") {...}
- otherwise, create some kind of collection (perhaps a
HashSet<string>
) and useif (options.Contains(value)) {...}
英文:
No. A string is one value. If you want to test whether a string is something or something else - or one of some set of options, then:
- for constant strings, you can use
if (value is "abc" or "def") {...}
- otherwise, create some kind of collection (perhaps a
HashSet<string>
) and useif (options.Contains(value)) {...}
答案2
得分: 2
你似乎想要能够在同一个变量中存储两个值。对此,你不需要使用字符串。我建议你使用一个List<string>()
List<string> joints = new List<string>();
然后添加到列表中,
joints.Add("Press Joint");
joints.Add("ProPress");
要检查是否存在匹配,
if (joints.Contains(brazed))
{
//进行一些操作
}
希望这能帮到你。
英文:
You appear to want to be able to store two values in the same variable. You don't want a string for that. I would suggest you use a List<string>()
List<string> joints = new List<string>();
Then to add to the list,
joints.Add("Press Joint");
joints.Add("ProPress");
To check for a match,
if (joints.Contains(brazed)
{
//do something
}
I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论