J’ai un cylindre et j’ai créé une face circulaire à l’aide d’un outil de chaîne. Je veux simuler le cylindre pour le déplacement et dans cette étape, je veux appliquer une force sur la face circulaire que j’ai sélectionnée. Je veux appliquer une force uniforme dans la direction Z du système de coordonnées d’environ 400N. Lorsque j’exécute ce code, j’ai obtenu le code d’erreur 6 pour l’AddForce3. Quelqu’un pourrait-il m’aider s’il vous plaît ? Je l’ai fait en C#. J’essaie d’abord avec un visage et une force. Je le ferai avec plusieurs visages et force quand cela fonctionnera. J’ai donné l’image du cylindre ci-dessous.
using SldWorks;
using SolidWorks.Interop.cosworks;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.IO;
using System.Threading;
using ISldWorks = SolidWorks.Interop.sldworks.ISldWorks;
using ModelDoc2 = SolidWorks.Interop.sldworks.ModelDoc2;
using SelectionMgr = SolidWorks.Interop.sldworks.SelectionMgr;
class Program
{
static void Main()
{
Console.WriteLine("Starting SolidWorks...");
var swApp = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as ISldWorks;
if (swApp == null)
{
Console.WriteLine("Failed to start SolidWorks.");
return;
}
swApp.Visible = true;
Console.WriteLine("SolidWorks launched.");
Thread.Sleep(5000);
string partFilePath = @"C:\Users\Student\Tasks\API Tasks\L1_4 Task07.SLDPRT";
if (!System.IO.File.Exists(partFilePath))
{
Console.WriteLine("File not found: " + partFilePath);
return;
}
int errors = 0, warnings = 0;
ModelDoc2 model = swApp.OpenDoc6(partFilePath,
(int)swDocumentTypes_e.swDocPART,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"", ref errors, ref warnings);
if (model == null)
{
Console.WriteLine("Failed to open part file.");
return;
}
Console.WriteLine("Part file opened.");
Thread.Sleep(2500);
string simDllPath = @"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\Simulation\cosworks.dll";
int loadResult = swApp.LoadAddIn(simDllPath);
Console.WriteLine($"Simulation Add-in load result: {loadResult}");
var simAddIn = swApp.GetAddInObject("CosmosWorks.CosmosWorks") as CwAddincallback;
if (simAddIn == null)
{
Console.WriteLine("Failed to load Simulation Add-in object.");
return;
}
CosmosWorks cosmos = simAddIn.CosmosWorks;
CWModelDoc simDoc = cosmos?.ActiveDoc;
if (simDoc == null)
{
Console.WriteLine("Simulation document not active.");
return;
}
Thread.Sleep(1500);
ICWStudyManager studyMgr = simDoc.StudyManager;
studyMgr.ActiveStudy = 0;
CWStudy study = studyMgr.GetStudy(0);
if (study == null)
{
Console.WriteLine("No study found.");
return;
}
Thread.Sleep(1500);
// ==================== APPLY FORCE LOAD ====================
Console.WriteLine("Applying force load...");
// Get ModelDocExtension for face selection
object selFace = null;
SelectionMgr swSelMgr = model.SelectionManager;
model.Extension.SelectByID2("", "FACE", 4.848210781, -0.473088237, 1.944270691, false, 0, null, 0);
selFace = (object)swSelMgr.GetSelectedObject6(1, -1);
if (selFace == null)
{
Console.WriteLine("Failed to select face. Check face name.");
}
else
{
// Get Loads and Fixtures manager
ICWLoadsAndRestraintsManager loadMgr = study.LoadsAndRestraintsManager;
if (loadMgr == null)
{
Console.WriteLine("Failed to get Loads/Fixtures manager.");
}
else
{
int loadErr = 0;
double[] data = new double[6];
// Create the force
data[0] = 1.0;
data[1] = 1.0;
data[2] = 1.0;
data[3] = 1.0;
data[4] = 1.0;
data[5] = 1.0;
object[] forceArray = { data[0], data[1], data[2], data[3], data[4], data[5] };
CWForce force = loadMgr.AddForce3(
(int)swsForceType_e.swsForceTypeNormal, // Load type: force
(int)swsSelectionType_e.swsSelectionFaceEdgeVertexPoint, // Apply on faces
-1, // No reference direction
0, // Not table-driven
0, // Not table-driven
0, // No rows
new object[0], // DistValue irrelevant
new object[0], // ForceValue irrelevant
false, // Uniform loading
false, // Not a beam
0, // No non-uniform def
0, // No non-uniform type
4, // Ucode: 0 = Normal
400.0, // Force magnitude (N)
(forceArray), // Comps array [Fx, Fy, Fz...]
false, // FlipOrigin = false
false, // Not curved beam
null, // DispArray not used
selFace, // RefGeom not used
false, // PerUnitLength = false
out loadErr // Error code
);
if (force == null || loadErr != 0)
{
Console.WriteLine($"Failed to apply force. Error code: {loadErr}");
}
else
{
Console.WriteLine("Force of 400 N in +Z applied successfully.");
}
}
}
Console.WriteLine("Done. Press any key to exit.");
Console.ReadKey();
}
}