Mam walec i utworzyłem okrągłą powierzchnię za pomocą narzędzia do wypaczania. Chcę zasymulować cylinder pod kątem przemieszczenia i na tym etapie chcę przyłożyć siłę do wybranej przeze mnie powierzchni kołowej. Chcę przyłożyć jednostajną siłę w kierunku Z układu współrzędnych około 400N. Po uruchomieniu tego kodu otrzymałem kod błędu 6 dla AddForce3. Czy ktoś mógłby mi pomóc? Zrobiłem to w C#. Początkowo staram się jedną twarzą i siłą. Zrobię to z wieloma twarzami i siłą, kiedy to się uda. Poniżej podałem zdjęcie cylindra.
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();
}
}